public class ThreadTest
{
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
private int m=0;
public ThreadTest()
{
t1.start();
t2.start();
}
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
}
class Thread1 extends Thread
{
public void run()
{
while(true)
{ m++;
System.out.println("线程1");
try {
if(t2.isAlive())
t2.wait();
if(m>5)
{
t1.wait();
t2.notify();
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Thread2 extends Thread
{
public void run()
{
while(true)
{
System.out.println("线程2");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
先让第一个线程执行,再让第二个线程执行!