多线程
线程交替打印
public static int count = 0;
public static int max = 100;
public static void main(String[] args) {
Object lock = new Object();
Runnable runnable = () -> {
synchronized (lock) {
while (count < max) {
System.out.println(Thread.currentThread().getName() + count++);
lock.notify();
if (count == max) return;
lock.wait();
}
}
};
new Thread(runnable, "奇数").start();
new Thread(runnable, "偶数").start();
}
Comments NOTHING