【发布时间】:2013-10-02 04:49:04
【问题描述】:
我有一个应用程序并行执行两个线程,每个线程执行一些操作,最终生成一个整数。在我的主线程中,我想对每个线程 A 和线程 B 产生的 int 执行一些操作(分别称为 int a 和 int b),然后按照每个线程吐出它们的顺序输出它们。所以本质上,我定期运行每个线程(比如每 2 秒),每个线程都会吐出一个整数,我按照它们的创建顺序操作和打印出来。
为了做到这一点,我如何将需要由 gui 线程打印的每个线程的 int 聚合在一起?我可以为主线程观察到的每个线程使用一个队列,当它更新时,主 gui 操作并输出它们?这样的队列怎么写?
我的线程代码如下:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
new Thread(new t1()).start(//TODO: pass in some parameter that this
//thread will write to);
new Thread(new t2()).start();
}
}, 0, period);
【问题讨论】:
-
队列,等待()和通知()。这些是您的关键字。现在没时间详细说明,抱歉。
标签: java multithreading