package com.test;
public class ThreadTest2 {
private static byte[] lock = new byte[0];
private static int i = 0;
public static void main(String[] args) {
new Thread() {
public void run() {
while (true) {
synchronized (lock) {
if (i % 2 == 0) {
System.out.println(Thread.currentThread().getName()+"-" + i++);
try {
lock.wait();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
}.start();
new Thread() {
public void run() {
while (true) {
synchronized (lock) {
if (i % 2 != 0) {
System.out.println(Thread.currentThread().getName()+"-" + i++);
lock.notifyAll();
}
}
}
};
}.start();
}
}