package com.yjw.thread;
public class Test2 {
static class Businis {
boolean f = false;// true sub执行,false main执行
public synchronized void sub(int i) {
if (!f) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 10; j++) {
System.out.println("sub=" + j + ",loop=" + i);
}
f = false;
this.notify();
}
public synchronized void main(int i) {
if (f) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 100; j++) {
System.out.println("main=" + j + ",loop=" + i);
}
f = true;
this.notify();
}
}
public static void main(String[] args) {
final Businis businis = new Businis();
for (int i = 1; i <= 50; i++) {
businis.main(i);
businis.sub(i);
}
}
}