public class BlockJoin {
public static void main(String[] args) {
new Thread(new father()).start();
}
}
class father implements Runnable{
@Override
public void run() {
System.out.println("老爸想吸烟");
System.out.println("老爸给儿子钱,儿子去买烟");
System.out.println("儿子发现游戏机,儿子玩了一会");
Thread s = new Thread(new son());
s.start();
try {
s.join();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("儿子走丢了。。。");
}
System.out.println("老爸接过了烟,把零钱给了儿子");
}
}
class son implements Runnable{
@Override
public void run() {
System.out.println("儿子接过了钱,发现路边有游戏机");
for (int i = 1; i <= 10; i++) {
System.out.println("儿子玩了" + i + "分钟。。。");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("想起要买烟,拿着烟回家去。。。");
}
}
运行结果
老爸想吸烟
老爸给儿子钱,儿子去买烟
儿子发现游戏机,儿子玩了一会
儿子接过了钱,发现路边有游戏机
儿子玩了1分钟。。。
儿子玩了2分钟。。。
儿子玩了3分钟。。。
儿子玩了4分钟。。。
儿子玩了5分钟。。。
儿子玩了6分钟。。。
儿子玩了7分钟。。。
儿子玩了8分钟。。。
儿子玩了9分钟。。。
儿子玩了10分钟。。。
想起要买烟,拿着烟回家去。。。
老爸接过了烟,把零钱给了儿子