package cn.lmj201402;
public class TraditionalThreadCommunication
{
public static void main(String[] args)
{
final Business business = new Business();
new Thread(new Runnable()
{
@Override
public void run()
{
for(int i = 0;i<20;i++)
{
business.main();
}
}
}).start();
for(int i = 0;i<20;i++)
{
business.sub();
}
}
}
class Business
{
boolean b = true;
public synchronized void main()
{
if(!b)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for(int i = 0;i<10;i++)
{
System.out.println("Main "+i);
}
b = false;
notify();
}
public synchronized void sub()
{
if(b)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for(int i = 0;i<5;i++)
{
System.out.println("Sub"+i);
}
b = true;
notify();
}
}
public class TraditionalThreadCommunication
{
public static void main(String[] args)
{
final Business business = new Business();
new Thread(new Runnable()
{
@Override
public void run()
{
for(int i = 0;i<20;i++)
{
business.main();
}
}
}).start();
for(int i = 0;i<20;i++)
{
business.sub();
}
}
}
class Business
{
boolean b = true;
public synchronized void main()
{
if(!b)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for(int i = 0;i<10;i++)
{
System.out.println("Main "+i);
}
b = false;
notify();
}
public synchronized void sub()
{
if(b)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for(int i = 0;i<5;i++)
{
System.out.println("Sub"+i);
}
b = true;
notify();
}
}