public class Test {
private Integer result=0;
private int numVlaue=9;
private Integer num = 0;
public static void main(String[] args) {
Test tt=new Test();
tt.A();
tt.B();
}
public void B() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("B线程准备");
{
System.out.println("B线程开始");
while(true)
{
if(num==numVlaue){
{
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (result)
{
System.out.println("numb"+num);
result.notify();
num=0;
break;
}
}
}
else{
if(num==9){
num=0;
}else{
num++;
}
continue;
}
}
}
}
}).start();
}
public void A() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("A线程准备");
synchronized (result) {
System.out.println("A线程开始");
try {
result.wait(5000);
System.out.println("等待结束" +result++);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("a" +result++);
}
}
}
}).start();
}
}