两个线程,一个线程输出1,一个线程输出2,循环输出

来源:互联网 发布:软件测试培训 深圳 编辑:程序博客网 时间:2024/06/05 08:41
/** * 两个线程,一个线程输出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 {@Overridepublic 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 {@Overridepublic void run() {synchronized (SleepAndWaitThread2.class) {while (true) {System.out.println("2");SleepAndWaitThread2.class.notify();try {SleepAndWaitThread2.class.wait();} catch (InterruptedException e) {e.printStackTrace();}}}}}

0 1
原创粉丝点击