/**
*
* @author vlinux
*/
public class ThreadTest {
Thread1 live41=new Thread1();
Thread2 knocker=new Thread2();
private int m=0;
public ThreadTest() {
live41.setName("live41");
knocker.setName("knocker");
live41.start();
knocker.start();
}
public static void main(String[] args) {
ThreadTest t=new ThreadTest();
}
/**
* live41
*/
class Thread1 extends Thread {
public void run() {
System.out.println( "["+this.getName()+"]:\t我在等"+knocker.getName()+"那白痴." );
synchronized( knocker ) {
try {
knocker.wait(); //等knocker那头猪
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
//live41的责任是就数数字
System.out.println("["+this.getName()+"]:\t我靠!"+knocker.getName()+"那白痴终于干完了,轮到我了.");
System.out.print("["+this.getName()+"]:\t");
for( int i=0; i<10; i++ ) {
System.out.print( i+" " );
}
System.out.println(this.getName()+"的工作完成");
}
}
/**
* knocker
*/
class Thread2 extends Thread {
public void run() {
System.out.println( "["+this.getName()+"]:\t我10秒后开始运行." );
try {
Thread.sleep(1000*10); //等待10秒....恶~
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//knocker的工作就是念字母
System.out.print("["+this.getName()+"]:\t");
for( int i='a'; i<='z'; i++ ) {
System.out.print( (char)i+" " );
}
System.out.println(this.getName()+"的工作完成");
synchronized( this ) {
notify(); //通知live41,工作完成,开始干活
}
}
}
}