/**
* 两个线程,一个线程输出1,一个线程输出2,
*
* @author ffr@cnic.cn
*
*/
public class SleepAndWaitThread2 {
public static void main(String[] args) {
OneThread one = new OneThread();
TwoThread two = new TwoThread();
one.start();
two.start();
}
}
class OneThread extends Thread {
@Override
public void run() {
synchronized (SleepAndWaitThread2.class) {
while (true) {
System.out.println("1");
try {
SleepAndWaitThread2.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
SleepAndWaitThread2.class.notify();
}
}
}
}
class TwoThread extends Thread {
@Override
public void run() {
synchronized (SleepAndWaitThread2.class) {
while (true) {
System.out.println("2");
SleepAndWaitThread2.class.notify();
try {
SleepAndWaitThread2.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
两个线程,一个线程输出1,一个线程输出2,循环输出
最新推荐文章于 2022-06-05 20:03:43 发布