标签:stack err on() ring 阻塞 ext except ted 通过
joinpublic class n {
public static void main(String[]args) throws InterruptedException
{
Thread t =new Thread(()-> {
for(int i=0;i<5;i++)
{
System.out.println("a"+i);
}
});
t.start();
for(int i=0;i<5;i++)
{
if(i%2==0)
{
t.join();//插队,此时main主线程被阻塞,插队线程执行完所有步骤再执行main
}
System.out.println("b"+i);
}
}
}
例二:
public class n {
public static void main(String[]args) throws InterruptedException
{
new Thread(new father()).start();
}
}
class father extends Thread{
public void run()
{
System.out.println("想抽象,发现没了");
System.out.println("让儿子买中华");
Thread t=new Thread(new son());
t.start(); //不行,各走各的逻辑错误,再加入join先执行完son,再执行father剩下的
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("接过烟");
}
}
class son extends Thread{
public void run()
{
System.out.println("拿钱");
System.out.println("路边玩10秒");
for(int i=0;i<10;i++)
{
System.out.println(i+"秒过去了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("去买烟");
System.out.println("回家");
}
}
标签:stack err on() ring 阻塞 ext except ted 通过
原文地址:https://blog.51cto.com/14437184/2428884