class A extends Thread { void a(){ while(true) { synchronized(Main.lock){ while(Main.state != Main.A_RUNNING) Main.lock.wait(); } //do sth here } } public void run(){ a(); } }; class B extends Thread { void b(){ while(true) { synchronized(Main.lock){ while(Main.state != Main.B_RUNNING) Main.lock.wait(); } //do sth here } } public void run(){ b(); } }; public class Main{ static final byte START = 0; static final byte A_RUNNING = 1; static final byte B_RUNNING = 2; static byte state = START; static Object lock = new Object(); public static void main(String[] args) throws Exception { A a = new A(); B b = new B(); a.start(); b.start(); while(true){ //通知A运行 state = A_RUNNING; synchronized(lock){ lock.notifyAll(); } Thread.sleep(10000);//休眠 //通知B运行 state = B_RUNNING; synchronized(lock){ lock.notifyAll(); } Thread.sleep(10000);//休眠 } } }