package practice;
import java.io.IOException;
public class Main {
static final Object object = new Object();
public static void main(String[] args) throws InterruptedException {
new Thread(new Runnable() {
String a[] = {"a","b"};
@Override
public void run() {
for (int i = 0; i < 2; i++) {
synchronized (object){
System.out.println("xiancheng a START");
object.notify();
try {
System.out.println("a wait");
object.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("a 继续执行");
System.out.println(a[i]);
System.out.println("a end");
object.notify();
}
}
}
}).start();
new Thread(new Runnable() {
int[] a = {1,2};
@Override
public void run() {
for (int i = 0; i < 2; i++) {
synchronized (object){
System.out.println("xiancheng 1 START");
object.notify();
try {
System.out.println("1 wait");
object.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("1 继续执行");
System.out.println(a[i]);
System.out.println("1 end");
object.notify();
}
}
}
}).start();
}
}