我有一个关于并发的简单问题。我正在通过可运行接口(interface)实现线程和并发。如果我首先初始化线程,然后在初始化后单独调用 start,或者如果我初始化线程并从同一个 for 循环中调用 start,有什么区别吗?
这是一个例子
for (int i= 0; i < threads.length; i++)
threads[i]= new
Thread(new RunnableThread(this, urls[i]));
for (Thread thread : threads)
thread.start();
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
}
}
或
for (int i= 0; i < threads.length; i++) {
threads[i]= new
Thread(new RunnableThread(this, urls[i]));
threads[i].start();
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
}
}
最佳答案
没有区别,您无法预测在这两种情况下哪个线程将启动。
关于java - 并发性 - 初始化启动线程之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27448063/