这里先实现两个线程轮流打印1-100数字,即线程一打印1,3,5,…,线程二打印2,4,6,…。
class PrintThread implements Runnable {
private static Object lock = new Object();
private int number;
public PrintThread(int number) {
this.number = number;
}
@Override
public void run() {
while (number <= 100) {
try {
synchronized (lock) {
System.out.println(number + " " + Thread.currentThread());
lock.notify()