import java.lang.Runnable;
import java.lang.Thread;public class bean8 implements Runnable{ public bean8() {
TestThread testthread1 = new TestThread(this,"1");
TestThread testthread2 = new TestThread(this,"2"); testthread2.start();
testthread1.start();
} public static void main(String[] args) throws Exception {
new bean8();
}
public void run(){ TestThread t = (TestThread) Thread.currentThread();
try{
if (!t.getName().equalsIgnoreCase("1")) {
synchronized(this) {
wait();
}
}
while(true){ System.out.println("@time in thread"+ t.getName()+ "="+ t.increaseTime()); if(t.getTime()%10 == 0) {
synchronized(this) {
System.out.println("****************************************");
notify();
if ( t.getTime()==100 ) break;
wait();
}
}
}
}catch(Exception e){e.printStackTrace();}
}}class TestThread extends Thread{
private int time = 0 ;
public TestThread(Runnable r,String name){
super(r,name);
}
public int getTime(){
return time;
}
public int increaseTime (){
return ++time;
}
}
import java.lang.Thread;public class bean8 implements Runnable{ public bean8() {
TestThread testthread1 = new TestThread(this,"1");
TestThread testthread2 = new TestThread(this,"2"); testthread2.start();
testthread1.start();
} public static void main(String[] args) throws Exception {
new bean8();
}
public void run(){ TestThread t = (TestThread) Thread.currentThread();
try{
if (!t.getName().equalsIgnoreCase("1")) {
synchronized(this) {
wait();
}
}
while(true){ System.out.println("@time in thread"+ t.getName()+ "="+ t.increaseTime()); if(t.getTime()%10 == 0) {
synchronized(this) {
System.out.println("****************************************");
notify();
if ( t.getTime()==100 ) break;
wait();
}
}
}
}catch(Exception e){e.printStackTrace();}
}}class TestThread extends Thread{
private int time = 0 ;
public TestThread(Runnable r,String name){
super(r,name);
}
public int getTime(){
return time;
}
public int increaseTime (){
return ++time;
}
}
相对应的有notify(叫醒一个类)与notifyAll(叫醒所有类)