【发布时间】:2014-06-24 11:46:47
【问题描述】:
我需要同时执行一组线程并等待它们完成,然后再恢复主线程。这是我的自定义类 MyRunnable:
Class MyRunnable implements Runnable{
int value;
MyRunnable(int value){
this.value = value;
}
@Override
public void run(){
System.out.println("Started thread " + value);
System.out.println("Finished thread " + value);
}
}
Thread[] myThreads;
int total = 25;
int size = 5;
while(total > 0){
total -= 5;
size = 5;
myThreads = new Thread[size];
for(int i = 0; i < size; i++){
myThreads[i] = new Thread(new MyRunnable (i));
}
for(Thread t:myThreads){
T.start()j
}
for(Thread t:myThreads){
try{
t.join();
} catch(InterruptedException ex) {
System.out.println("error");
}
}
System.out.println("set finished");
}
我需要一组五个线程一次执行,并且主线程只有在所有线程都完成执行后才能继续。上面的代码不起作用,因为对 .join 的调用似乎被忽略了。我还尝试给组中的每个线程一个随机的睡眠时间,以查看意外结果是否是由于线程完成的速度比调用 .join() 的速度更快。结果还是一样。
【问题讨论】:
-
是什么阻止你在这里使用
Executor? -
实际启动线程的代码行中有两个拼写错误,所以我怀疑这不是您的实际代码,并且您的真实版本中有一些我们不知道的错误。
-
我喜欢“
Class”的定义...从 MS Word 复制粘贴? -
for(Thread t:myThreads){T.start()j}->for(Thread t:myThreads){t.start();} -
我对代码进行了更正。它缺少一个主要方法。
标签: java multithreading