package TimerTest;
//子线程循环10次,主线程循环100次,接着又回到了子线程循环10次,然后再回到主线程,如此循环50次
public class TraditionalThreadCommunication {
public static void main(String[] args) throws InterruptedException {
Bussess1 bussess = new Bussess1();
new Thread(new Runnable() {
@Override
public void run() {
for (int i=1;i<=50;i++)
{
try {
bussess.secondThread(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
for (int i=1;i<=50;i++)
{
bussess.mainThread(i);
}
}
}
class Bussess1{
private boolean isFlag = true;
public synchronized void mainThread(int i) throws InterruptedException
{
while(isFlag)
{
this.wait();
}
for (int j=1;j<=10;j++)
{
System.out.println("主线程-"+j+"外部循环-"+i+"次");
}
isFlag = true;
this.notify();
}
public synchronized void secondThread(int i) throws InterruptedException
{
while(!isFlag)
{
this.wait();
}
for(int j=1;j<=100;j++)
{
System.out.println("子线程-"+j+"-,外部循环-"+i+"次");
}
isFlag = false;
this.notify();
}
}