62,614
社区成员
发帖
与我相关
我的任务
分享
public class Test
{
Object o = new Object();
boolean b = false;
public static void main(String[] args)
{
new Thread(new Thread1()).start();
new Thread(new Thread2()).start();
}
private class Thread1 implements Runnable
{
public void run()
{
a();
}
private void a() throws InterruptedException
{
synchronized(o)
{
if(b)
{
wait();
}
//执行内容
}
}
}
private class Thread2 implements Runnable
{
public void run()
{
b();
}
private void b() throws InterruptedException
{
b = true;
synchronized(o)
{
//执行内容
b = false;
notify();
}
}
}
}