使用两个线程交替打印 1-100,例子。
package com.comm;
/**
* 需求: 使用两个线程交替打印 1-100。
*/
class Print implements Runnable {
private int num = 1;
private Object obj = new Object();
@Override
public void run() {
while(true) {
synchronized (obj) {
obj.notify();
if (num <= 100) {
System.out.println(Thread.currentThread().getName() + " : " + num);
num++;
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
}
}
}
}
public class Communication {
public static void main(String[] args) {
Print p = new Print();
new Thread(p, "线程1").start();
new Thread(p, "线程2").start();
new Thread(p, "线程3").start();
}
}