java中的thread.join()问题
我的代码java中的thread.join()问题,java,multithreading,Java,Multithreading,我的代码 public class Main { public static void main(String[] args)throws Exception { Thread thread1=new Thread(new Runnable() { public void run() { System.out.println("Thread1"); }
public class Main {
public static void main(String[] args)throws Exception {
Thread thread1=new Thread(new Runnable()
{
public void run()
{
System.out.println("Thread1");
}
});
thread1.join();
thread1.start();
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
}
}
公共类主{
公共静态void main(字符串[]args)引发异常{
Thread thread1=新线程(new Runnable()
{
公开募捐
{
System.out.println(“螺纹1”);
}
});
thread1.join();
thread1.start();
对于(int i=0;i<1000;i++){
系统输出打印LN(i);
}
}
}
有时“Thread1”甚至在打印所有数字之前就打印出来了。有什么原因吗?Thread1不应该等到主线程完成吗
thread1.join();
thread1.start()
成功
thread1.start()
thread1.join();
它将通过thread1.start()
从主线程开始一个线程,主线程将继续在它将看到的下一行执行thread1.join()代码>将暂停主线程的执行,直到thread1完成。所以你的工作会完成的
成功
thread1.start()
thread1.join();
它将通过thread1.start()
从主线程开始一个线程,主线程将继续在它将看到的下一行执行thread1.join()代码>将暂停主线程的执行,直到thread1完成。因此,通过调用Thread.join()
当前线程将等待您调用的线程join()
,您的工作将完成
在您的情况下,您正在等待thread1
在启动之前完成
如果希望thread1
等待数字完成打印,可以使用倒计时闩锁
:
公共类主{
公共静态最终倒计时闩锁启动信号=新倒计时闩锁(1);
公共静态void main(字符串[]args)引发异常{
Thread thread1=新线程(new Runnable()
{
公开募捐
{
试一试{
开始信号。等待();
System.out.println(“螺纹1”);
}catch(InterruptedException ex){}
}
});
thread1.start();
对于(int i=0;i<1000;i++){
系统输出打印LN(i);
}
开始信号倒计时();
thread1.join();
}
}
通过调用Thread.join()
当前线程等待您调用的线程join()
在您的情况下,您正在等待thread1
在启动之前完成
如果希望thread1
等待数字完成打印,可以使用倒计时闩锁
:
公共类主{
公共静态最终倒计时闩锁启动信号=新倒计时闩锁(1);
公共静态void main(字符串[]args)引发异常{
Thread thread1=新线程(new Runnable()
{
公开募捐
{
试一试{
开始信号。等待();
System.out.println(“螺纹1”);
}catch(InterruptedException ex){}
}
});
thread1.start();
对于(int i=0;i<1000;i++){
系统输出打印LN(i);
}
开始信号倒计时();
thread1.join();
}
}
尽管您应该在thread1.join()之前调用thread1.start()
,但实际上您不知道线程将在何时执行。它可能发生在打印i
时,也可能发生在打印之前或之后。取决于操作系统的线程调度机制。正确的代码应如下所示:
public class Main {
public static void main(String[] args)throws Exception {
Thread thread1=new Thread(new Runnable()
{
public void run()
{
System.out.println("Thread1");
}
});
// start the thread
thread1.start();
// print the numbers... meanwhile your thread *could* get executed
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
// wait for thread1 to finish
thread1.join();
}
公共类主{
公共静态void main(字符串[]args)引发异常{
Thread thread1=新线程(new Runnable()
{
公开募捐
{
System.out.println(“螺纹1”);
}
});
//开线
thread1.start();
//打印数字…同时你的线程*可以*执行
对于(int i=0;i<1000;i++){
系统输出打印LN(i);
}
//等待thread1完成
thread1.join();
}
}尽管您应该在thread1.join()
之前调用thread1.start()
,但实际上您不知道线程何时执行。它可能发生在打印i
时,也可能发生在打印之前或之后。取决于操作系统的线程调度机制。正确的代码应如下所示:
public class Main {
public static void main(String[] args)throws Exception {
Thread thread1=new Thread(new Runnable()
{
public void run()
{
System.out.println("Thread1");
}
});
// start the thread
thread1.start();
// print the numbers... meanwhile your thread *could* get executed
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
// wait for thread1 to finish
thread1.join();
}
公共类主{
公共静态void main(字符串[]args)引发异常{
Thread thread1=新线程(new Runnable()
{
公开募捐
{
System.out.println(“螺纹1”);
}
});
//开线
thread1.start();
//打印数字…同时你的线程*可以*执行
对于(int i=0;i<1000;i++){
系统输出打印LN(i);
}
//等待thread1完成
thread1.join();
}
}实际上,在调用join()之前,我们应该确保系统已经按照我们的“.start()”请求启动了线程(这不一定发生在start()返回之前),因为join()实际上并不等待线程死亡,而是在线程未运行时立即返回。这可能发生在线程开始之前,也可能发生在线程结束之后。参见Allen Holub的“驯服Java线程”,第87页。以下是我认为编写此代码的正确方法:
导入java.util.concurrent.Semaphore
公共班机{
公共静态void main(字符串[]args)引发异常{
最终信号量waitForThreadToStart=新信号量(0);
Thread thread1=新线程(new Runnable()
{
公开募捐
{
waitForThreadToStart.release();
System.out.println(“螺纹1”);
}
});
thread1.start();
waitForThreadToStart.acquire();//等待线程启动
thread1.join();//现在进行连接。
对于(int i=0;i<10;i++){
系统输出打印LN(i);
}
}