我想打一下1,2,3。然后我这么写
System.out.println(1);
Thread thread=new Thread(new Runnable() {
public void run() {
System.out.println(2);
}
});
thread.start();
System.out.println(3);
结果打印出来是
1,3,2.我就纠结了,我只想他打印完2之后再执行打印3,怎么办
System.out.println(1);
Thread thread=new Thread(new Runnable() {
public void run() {
System.out.println(2);
}
});
thread.start();
try {
//等待线程执行完
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(3);
我加了一句,就打印1,2,3了,
thread.join();
是等待线程执行完的方法,加上这一句就OK了,记得try catch异常噢