/**
*
* 模拟烧茶的过程:
* 1)烧水
* 2)需要茶叶的时候发现没茶叶,叫eric去买(没有茶叶,需要买)
* 3)需要杯子的时候发现没杯子,叫meten去买(没有杯子,需要买)
* 4)放茶叶
* 5)倒水
* @author 够潮
*
*/
public class Demo12 {
/**
* @param args
*/
public static void main(String[] args) {
BurnTea bt = new BurnTea("够潮");
bt.start();
}
}
/**
* 管理线程
* @author 够潮
*
*/
class BurnTea extends Thread{
public BurnTea(String name){
super(name);
}
@Override
public void run() {
System.out.println(this.getName() +"烧水");
System.out.println(this.getName()+"发现没有茶叶了,叫eric去买");
TeaThread teaThread = new TeaThread("eric");
try {
teaThread.start();
teaThread.join();//买茶叶
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.getName()+"发现没有茶杯了,meten去买");
TeaCupThread teaCupThread = new TeaCupThread("meten");
try {
teaCupThread.start();
teaCupThread.join();//买茶杯
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.getName()+"放茶叶");
System.out.println(this.getName()+"倒水");
System.out.println(this.getName()+"烧好茶了");
}
}
/**
* 买茶叶模拟线程
* @author 够潮
*
*/
class TeaThread extends Thread{
public TeaThread(String name ){
super(name);
}
@Override
public void run() {
System.out.println(this.getName() +"去买茶叶啦");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.getName()+"买茶叶回来啦");
}
}
/**
* 没茶杯模拟线程
* @author 够潮
*
*/
class TeaCupThread extends Thread{
public TeaCupThread(String name ){
super(name);
}
@Override
public void run() {
System.out.println(this.getName() +"去买茶杯啦");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.getName()+"买茶杯回来啦");
}
}