【Java】【多线程】两个线程间的通信、wait、notify、notifyAll

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/love_Aym/article/details/79972168

等待唤醒机制:wait()、notify()

1、什么时候需要通信

  • 多个线程并发执行时, 在默认情况下CPU是随机切换线程的
  • 如果我们希望他们有规律的执行, 就可以使用通信, 例如每个线程执行一次打印,轮流执行

2、怎么通信

  • 如果希望线程等待, 就调用wait()
  • 如果希望唤醒等待的线程, 就调用notify();
  • 这两个方法必须在同步代码中执行, 并且使用同步锁对象来调用,this
public class Demo1_Notify {
	/**
	 * @param args
	 * 等待唤醒机制
	 */
	public static void main(String[] args) {
		final Printer p = new Printer();
		
		new Thread() {       //线程1
			public void run() {
				while(true) {
					try {
						p.print1();
					} catch (InterruptedException e) {
						
						e.printStackTrace();
					}
				}
			}
		}.start();
		
		new Thread() {   //线程2
			public void run() {
				while(true) {
					try {
						p.print2();
					} catch (InterruptedException e) {
						
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
}

//等待唤醒机制
class Printer {
	private int flag = 1;
	public void print1() throws InterruptedException {							
		synchronized(this) {
			if(flag != 1) {
				this.wait();	//当前线程等待,直到被唤醒
			}
			System.out.print("黑");
			System.out.print("马");
			System.out.print("程");
			System.out.print("序");
			System.out.print("员");
			System.out.print("\r\n");
			flag = 2;
			this.notify();		//随机唤醒单个等待的线程,此时仍然具有执行权,上面使得线程进入等待
		}
	}
	
	public void print2() throws InterruptedException {
		synchronized(this) {
			if(flag != 2) {
				this.wait();
			}
			System.out.print("传");
			System.out.print("智");
			System.out.print("播");
			System.out.print("客");
			System.out.print("\r\n");
			flag = 1;
			this.notify();
		}
	}
}

2、多个线程通信的问题

  • notify()方法是随机唤醒一个线程
  • notifyAll()方法是唤醒所有线程
  • JDK5之前无法唤醒指定的一个线程
  • 如果多个线程之间通信, 需要使用notifyAll()通知所有线程, 用while来反复判断条件
public class Demo2_NotifyAll {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Printer2 p = new Printer2();
		new Thread() {
			public void run() {
				while(true) {
					try {
						p.print1();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
		
		new Thread() {
			public void run() {
				while(true) {
					try {
						p.print2();
					} catch (InterruptedException e) {	
						e.printStackTrace();
					}
				}
			}
		}.start();
		
		new Thread() {
			public void run() {
				while(true) {
					try {
						p.print3();
					} catch (InterruptedException e) {						
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
}

class Printer2 {
	private int flag = 1;
	public void print1() throws InterruptedException {							
		synchronized(this) {
			while(flag != 1) {
				this.wait();	//当前线程等待
			}
			System.out.print("黑");
			System.out.print("马");
			System.out.print("程");
			System.out.print("序");
			System.out.print("员");
			System.out.print("\r\n");
			flag = 2;
			//this.notify();	//随机唤醒单个等待的线程
			this.notifyAll();
		}
	}
	
	public void print2() throws InterruptedException {
		synchronized(this) {
			while(flag != 2) {
				this.wait();	//线程2在此等待
			}
			System.out.print("传");
			System.out.print("智");
			System.out.print("播");
			System.out.print("客");
			System.out.print("\r\n");
			flag = 3;
			//this.notify();
			this.notifyAll();
		}
	}
	
	public void print3() throws InterruptedException {
		synchronized(this) {
			while(flag != 3) {
				this.wait();	//线程3在此等待,if语句是在哪里等待,就在哪里起来
						//while循环是循环判断,每次都会判断标记
			}
			System.out.print("i");
			System.out.print("t");
			System.out.print("h");
			System.out.print("e");
			System.out.print("i");
			System.out.print("m");
			System.out.print("a");
			System.out.print("\r\n");
			flag = 1;
			//this.notify();
			this.notifyAll();
		}
	}
}

三、wait()和notify()的通常用法

Java多线程开发中,我们常用到wait()和notify()方法来实现线程间的协作,简单的说步骤如下: 
1. A线程取得锁,执行wait(),释放锁; 
2. B线程取得锁,完成业务后执行notify(),再释放锁; 
3. B线程释放锁之后,A线程取得锁,继续执行wait()之后的代码;

注意:wait(long timeout):让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的notify()方法或 notifyAll() 方法,或者超过指定的时间量”,当前线程被唤醒(进入“就绪状态”)。


四、通信需要注意的几点:

1、在同步代码块中,用哪个对象锁,就用哪个对象调用wait方法:this.wait()

2、为什么wait方法和notify方法定义在Object这类中?

因为锁对象可以是任意对象,Object是所有的类的基类,所以wait方法和notify方法需要定义在Object这个类中



多线程的问题wait,notify

03-28

各位大神帮我看一道问题:rn关于wait和notify的,太纠结了,代码如下:rn[code=java]rnclass ThreadDemornrn public static void main(String[] args) rn ThreadA a = new ThreadA();rn ThreadB b = new ThreadB();rn a.setName("第A个线程");rn b.setName("第B个线程");rn b.start();rn System.out.println(Thread.currentThread().getName() + "is starting...");rn synchronized(b)rn rn tryrn rn System.out.println("Waiting a to complete....");rn b.wait();rn System.out.println("b colpleted , now back to " + Thread.currentThread().getName());rn a.start();rn rn catch (Exception e)rn rn System.out.println("IO");rn rn rn rnrnrnclass ThreadA extends Threadrnrn private int total;rn public void run()rn rn synchronized(this)rn System.out.println(Thread.currentThread().getName()+ " is running ...");rn for(int i = 0; i < 10; i++)rn rn total += i;rn rn System.out.println("total = " + total);rn rn rn rnrnrnclass ThreadB extends Threadrnrn private int total = 1;rn public void run()rn rn synchronized(this)rn rn System.out.println(Thread.currentThread().getName()+ " is running ...");rn for(int i = 1; i < 10; i++)rn rn total *= i;rn rn System.out.println("total = " + total);rn System.out.println("下面执行notify");rn notify();rn System.out.println("notify ok");rn rn rn rnrn[/code]rn[img=http://img.my.csdn.net/uploads/201303/28/1364438863_3971.JPG][/img][img=http://img.my.csdn.net/uploads/201303/28/1364438871_4263.JPG][/img]rn同一个程序,每次执行的结果不同,分别为以上图片中的两种结果。。。rn对于第一个结果来说,notify在wait之前进行的,为什么程序会执行完成了?rn附加问题:线程A wait之后,需要由其他的线程notify还是由自己notify?

多线程错误wait notifyAll错误

08-13

package com.xiaonei2shou.test.llThread;rnrnimport java.util.LinkedList;rnrnpublic class ThreadTest rn private static LinkedList booleanList = new LinkedList();rn private static boolean[] needWait = true ;rn static rn if (booleanList.size() <= 0) rn for (int i = 0; i < 1000; i++) rn booleanList.add(false);rn rn rn rnrn class OperateThread extends Thread rn public OperateThread(String name) rn super(name);rn rnrn public void run() rn synchronized (needWait) rn while (!booleanList.getFirst()) rn System.out.println(getName() + "开始执行");rn if (needWait[0]) rn System.out.println(getName() + "开始等待");rn try rn needWait.wait();rn catch (InterruptedException e) rn e.printStackTrace();rn rn System.out.println(getName() + "结束等待");rn rn needWait[0] = true;rn Boolean nowBoolean = booleanList.getFirst();rn booleanList.removeFirst();rn needWait[0] = false;rn needWait.notifyAll();rn booleanList.addLast(!nowBoolean);rn System.out.println(getName() + "结束执行");rn rnrn rn rn rnrn public static void main(String[] args) rn ThreadTest t = new ThreadTest();rn Thread t1 = t.new OperateThread("线程1");rn Thread t2 = t.new OperateThread("线程2");rn Thread t3 = t.new OperateThread("线程3");rn t1.start();rn t2.start();rn t3.start();rn needWait.notifyAll();rn while (true) rn if (!t1.isAlive() && !t2.isAlive() && !t3.isAlive()) rn System.out.println(booleanList.size());rn System.out.println(booleanList);rn break;rn rn rn rnrnrnrn[color=#FF0000]报错了[/color]rnrn[color=#FF0000]Exception in thread "main"[/color] 线程1开始执行rn线程1开始等待[color=#FF0000]java.lang.IllegalMonitorStateExceptionrn at java.lang.Object.notifyAll(Native Method)rn at com.xiaonei2shou.test.llThread.ThreadTest.main(ThreadTest.java:55)[/color]rnrn线程3开始执行rn线程3开始等待rn线程2开始执行rn线程2开始等待rnrn[color=#FF0000]怎么修改[/color]rnrn

notifynotifyAll

04-29

代码如下:rnpublic class Philosopher rn public static void main(String [] args)rn ChopStick chopstick_1=new ChopStick("1号筷子");rn ChopStick chopstick_2=new ChopStick("2号筷子");rn ChopStick chopstick_3=new ChopStick("3号筷子");rn ChopStick chopstick_4=new ChopStick("4号筷子");rn Philosopher_Using pu_1=new Philosopher_Using("哲学家_1",chopstick_1,chopstick_2);rn Philosopher_Using pu_2=new Philosopher_Using("哲学家_2",chopstick_2,chopstick_3);rn Philosopher_Using pu_3=new Philosopher_Using("哲学家_3",chopstick_3,chopstick_4);rn Philosopher_Using pu_4=new Philosopher_Using("哲学家_4",chopstick_4,chopstick_1);rn Thread tpu_1=new Thread(pu_1);rn Thread tpu_2=new Thread(pu_2);rn Thread tpu_3=new Thread(pu_3);rn Thread tpu_4=new Thread(pu_4);rn tpu_1.start();rn tpu_2.start();rn tpu_3.start();rn tpu_4.start();rn rn rnrnclass ChopStickrn protected String chopName;rn private boolean available=true;rn public ChopStick(String name)rn chopName=name;rn rn public void getChopStick()rn synchronized(this)rn tryrn while(available!=true)rn this.wait();rn rn available=false;rn catch(Exception e)rn System.out.println("ERROR!");rn rn rn rn rn rn public void releaseChopStick()rn synchronized(this)rn available=true;rn [color=#0000FF]notify();[/color]rn rn rn public void thinking(String name)rn System.out.println(name+"is thinking");rn rn rn rn public void eatMeal(String name)rn System.out.println(name+"is eating");rn rn rnrnclass Philosopher_Using implements Runnablern private String p_name;rn ChopStick stickLeft;rn ChopStick stickRight;rn public Philosopher_Using(String name,ChopStick stickLeft,ChopStick stickRight)rn p_name=name;rn this.stickLeft=stickLeft;rn this.stickRight=stickRight;rn rn public void run()rn stickLeft.thinking(p_name);rn tryrn Thread.currentThread();rn Thread.sleep(500);rn catch(Exception e);rn stickLeft.getChopStick();rn System.out.println(p_name+"抓起了"+stickLeft.chopName);rn stickRight.getChopStick();rn System.out.println(p_name+"抓起了"+stickRight.chopName);rn stickRight.eatMeal(p_name);rn tryrn Thread.currentThread();rn Thread.sleep(500);rn catch(Exception e);rn stickLeft.releaseChopStick();rn System.out.println(p_name+"放下了"+stickLeft.chopName);rn stickRight.releaseChopStick();rn System.out.println(p_name+"放下了"+stickRight.chopName);rn rnrnrn输出结果:rn哲学家_1is thinkingrn哲学家_4is thinkingrn哲学家_3is thinkingrn哲学家_2is thinkingrn哲学家_1抓起了1号筷子rn哲学家_1抓起了2号筷子rn哲学家_1is eatingrn哲学家_3抓起了3号筷子rn哲学家_4抓起了4号筷子rn哲学家_1放下了1号筷子rn哲学家_4抓起了1号筷子rn哲学家_4is eatingrn[color=#FF0000]哲学家_2抓起了2号筷子rn哲学家_1放下了2号筷子[/color]rn哲学家_4放下了4号筷子rn哲学家_3抓起了4号筷子rn哲学家_3is eatingrn哲学家_4放下了1号筷子rn哲学家_3放下了3号筷子rn哲学家_2抓起了3号筷子rn哲学家_3放下了4号筷子rn哲学家_2is eatingrn哲学家_2放下了2号筷子rn哲学家_2放下了3号筷子rnrn用notify的时候总会有这样的异常现象。还没放下筷子,就会先抓起。是什么原因呢?rn改notifyAll的话倒是正常的。不知道两者区别在哪里?

java线程 wait notify

09-24

源码如下:rn[code=java]rnpublic class TakeAndPour rn public static void main(String args[]) rn WaterTaking wt = new WaterTaking();rn WaterPouring wp = new WaterPouring();rn Thread t1 = new Thread(wt, "Xiaohao");rn Thread t2 = new Thread(wp, "doubi");rn t1.start();rn t2.start();rn rnrnclass Tank rn private static int count = 10; // 缸里原有的水rn //private static int max = 5;rn private static boolean flag = true; //防止同时取水、 倒入水rn public void takeWater() rn synchronized (this) rn while (!flag) rn try rn System.outrn .println("The tank is being used now");rn this.wait();rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn flag = false; //同一时刻只允许一个对水缸操作rn try rn Thread.sleep(800);rn catch (InterruptedException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn count--;rn System.out.println(flag+" "+Thread.currentThread().getName()rn + " take out some water" + " now water remain " + count);rn rnrn flag = true; rn this.notifyAll(); //操作完了就把flag置为true,并唤醒线程,这里使用notify()一样。rn rn rn rn public void pourWater() rn synchronized (this) rn flag = false;rn while (!flag) rn try rn System.outrn .println("the tank is being used");rn this.wait();rn catch (Exception e) rn e.printStackTrace();rn rn rn try rn Thread.sleep(800);rn catch (InterruptedException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn count++;rn System.out.println(flag+" "+Thread.currentThread().getName()rn + " pour in some water" + " now water remain " + count);rn flag = true;rn this.notifyAll();rn rn rn rnrnrnclass WaterTaking implements Runnable rn Tank t = new Tank();rnrn public void run() rn while(true)rn t.takeWater();rn rn rnrnclass WaterPouring implements Runnable rn Tank t = new Tank();rnrn public void run() rn while(true)rn t.pourWater();rn rn rnrn[/code]rnrn但是 现在的结果出现了问题,打印如下:rnthe tank is being used, or the tank is full of water.rnfalse Xiaohao take out some water now water remain 9rnfalse Xiaohao take out some water now water remain 8rnfalse Xiaohao take out some water now water remain 7rnfalse Xiaohao take out some water now water remain 6rnfalse Xiaohao take out some water now water remain 5rnfalse Xiaohao take out some water now water remain 4rnfalse Xiaohao take out some water now water remain 3rn.......rn倒入水的线程,第一下进去就被卡住了,全是取水的线程。求各位帮下忙分析下问题所在,谢了!

notify notifyall 很不理解

10-05

[code=Java]rnrn//代码1rnpublic class TestProducerConsumer rn public static void main(String[] args) rn SyncStack ss = new SyncStack();rn producer p = new producer(ss);rn customer c = new customer(ss);rn new Thread(p).start();rn new Thread(c).start();rn rnrnrnclass WoTou rn int id = 0;rnrn WoTou(int id) rn this.id = id;rn rnrn public String toString() rn return "WoTou" + id;rn rnrnrnclass SyncStack rn int index = 0;rn WoTou[] ArrWT = new WoTou[6];rnrn public synchronized void push(WoTou wt) rn if (index >= ArrWT.length) rn try rn System.out.println("满了,等待消费");rn wait();rn catch (InterruptedException e) rn e.printStackTrace();rn rn rn notify();rn ArrWT[index] = wt;rn index++;rn rnrn public synchronized WoTou pop() rn if (index == 0) rn try rn System.out.println("没WoTou了,等待生产");rn wait();rn catch (InterruptedException e) rn e.printStackTrace();rn rn rn notify();rn index--;rn return ArrWT[index];rnrn rnrnrnclass producer implements Runnable rn SyncStack ss = null;rnrn producer(SyncStack ss) rn this.ss = ss;rn rnrn public void run() rn for (int i = 0; i < 20; i++) rn WoTou wt = new WoTou(i);rn ss.push(wt);rn System.out.println("生产了:" + wt);rn try rn Thread.sleep(200);rn catch (Exception e) rn e.printStackTrace();rn rn rn rnrnrnclass customer implements Runnable rn SyncStack ss = null;rnrn customer(SyncStack ss) rn this.ss = ss;rn rnrn public void run() rn for (int i = 0; i < 20; i++) rn WoTou wt = ss.pop();rn System.out.println("消费了: " + wt);rn try rn Thread.sleep(1000);rn catch (Exception e) rn e.printStackTrace();rn rn rn rnrnrnrnrnrn/* //代码2rnpublic class TestProducerConsumer rnrn public static void main(String[] args) rn SyncStack ss = new SyncStack();rn producer p = new producer(ss);rn customer c = new customer(ss);rn new Thread(p).start();rn new Thread(c).start();rn rnrnrnclass WoTou rn int id = 0;rnrn WoTou(int id) rn this.id = id;rn rnrn public String toString() rn return "WoTou" + id;rn rnrnrnclass SyncStack rn int index = 0;rnrn WoTou[] ArrWT = new WoTou[6];rnrn public synchronized void push(WoTou wt) rn if (index >= ArrWT.length) rn try rn System.out.println("---------------满了,等待消费");rn wait();rn catch (InterruptedException e) rn e.printStackTrace();rn rnrn rn ArrWT[index] = wt;rn index++;rn notify();//执行完自己的代码才去唤醒别人,要不别人就会和你抢cpu资源。rnrn rnrn // 改变这无返回值rn public synchronized void pop() rn if (index == 0) rn try rn System.out.println("没WoTou了,等待生产");rn wait();rn catch (InterruptedException e) rn e.printStackTrace();rn rn rn notify();//虽然我唤醒了那些等待的线程,但线程我还是持有对象的锁!!!rn index--;rn // return ArrWT[index];rn // 改变如下:rn System.out.println("消费了: " + ArrWT[index]); rn rnrnrnclass producer implements Runnable rn SyncStack ss = null;rnrn producer(SyncStack ss) rn this.ss = ss;rn rnrn public void run() rn for (int i = 0; i < 20; i++) rn WoTou wt = new WoTou(i);rn ss.push(wt);rn System.out.println("生产了:" + wt);rn try rn Thread.sleep(200);rn catch (Exception e) rn e.printStackTrace();rn rn rn rnrnrnrnclass customer implements Runnable rn SyncStack ss = null;rnrn customer(SyncStack ss) rn this.ss = ss;rn rnrn public void run() rn for (int i = 0; i < 20; i++) rn // WoTou wt=ss.pop();//锁定了对象rn //执行到下面这句,你已经没有对象的锁!!!rn // System.out.println("消费了: "+wt);rn // 改变如下:rn ss.pop();//锁定了对象rn try rn Thread.sleep(1000);rn catch (Exception e) rn e.printStackTrace();rn rn rnrn rnrnrn*/rnrnrnrn[/code]rnrn看上面两段代码,rn第一段运行结果:rn生产了:WoTou0rn消费了: WoTou0rn生产了:WoTou1rn生产了:WoTou2rn生产了:WoTou3rn生产了:WoTou4rn消费了: WoTou4rn生产了:WoTou5rn生产了:WoTou6rn生产了:WoTou7rn满了,等待消费rn消费了: WoTou7rn生产了:WoTou8rn满了,等待消费rn消费了: WoTou8rn生产了:WoTou9rn满了,等待消费rn消费了: WoTou9rn生产了:WoTou10rn满了,等待消费rn消费了: WoTou10rn生产了:WoTou11rn满了,等待消费rn消费了: WoTou11rn生产了:WoTou12rn满了,等待消费rn消费了: WoTou12rn生产了:WoTou13rn满了,等待消费rn生产了:WoTou14rn消费了: WoTou13rn满了,等待消费rn生产了:WoTou15rn消费了: WoTou14rn满了,等待消费rn生产了:WoTou16rn消费了: WoTou15rn满了,等待消费rn消费了: WoTou16rn生产了:WoTou17rn满了,等待消费rn消费了: WoTou17rn生产了:WoTou18rn满了,等待消费rn消费了: WoTou18rn生产了:WoTou19rn消费了: WoTou19rn消费了: WoTou6rn消费了: WoTou5rn消费了: WoTou3rn消费了: WoTou2rn消费了: WoTou1rn出现了等待消费,接着又生产了,如WoTou15rnrn而第二段:rn生产了:WoTou0rn消费了: WoTou0rn生产了:WoTou1rn生产了:WoTou2rn生产了:WoTou3rn生产了:WoTou4rn消费了: WoTou4rn生产了:WoTou5rn生产了:WoTou6rn生产了:WoTou7rn---------------满了,等待消费rn消费了: WoTou7rn生产了:WoTou8rn---------------满了,等待消费rn消费了: WoTou8rn生产了:WoTou9rn---------------满了,等待消费rn消费了: WoTou9rn生产了:WoTou10rn---------------满了,等待消费rn消费了: WoTou10rn生产了:WoTou11rn---------------满了,等待消费rn消费了: WoTou11rn生产了:WoTou12rn---------------满了,等待消费rn消费了: WoTou12rn生产了:WoTou13rn---------------满了,等待消费rn消费了: WoTou13rn生产了:WoTou14rn---------------满了,等待消费rn消费了: WoTou14rn生产了:WoTou15rn---------------满了,等待消费rn消费了: WoTou15rn生产了:WoTou16rn---------------满了,等待消费rn消费了: WoTou16rn生产了:WoTou17rn---------------满了,等待消费rn消费了: WoTou17rn生产了:WoTou18rn---------------满了,等待消费rn消费了: WoTou18rn生产了:WoTou19rn消费了: WoTou19rn消费了: WoTou6rn消费了: WoTou5rn消费了: WoTou3rn消费了: WoTou2rn消费了: WoTou1rn就没问题rnrn谁能解释下。。。 希望大家能讲解下notify方法。rnrn

关于notifynotifyAll方法

12-22

看这段程序rnpackage org.arcie.simulator.test;rnrnpublic class NotifyTest implements Runnable rn rn private Wait wait; rn rn public NotifyTest(Wait wait) rn this.wait = wait; rn rn public void run() rn rn wait.doSomthing(); rn rn rn public static void main(String [] args) rn Wait wait = new Wait(4,"DAVID"); rn Thread t1 = new Thread(new NotifyTest(wait)); rn Thread t2 = new Thread(new NotifyTest(wait)); rn Thread t3 = new Thread(new NotifyTest(wait)); rn Thread t4 = new Thread(new NotifyTest(wait)); rn rn t1.start(); rn t2.start();rn t3.start(); rn t4.start(); rn rn rn rnrnclass Wait rn private int counter = 0; rn private String name = null; rn public Wait(int counter,String name) rn this.counter = counter; rn this.name = name; rn rn rn public synchronized void doSomthing() rn int tempCounter = --counter; rn if(tempCounter <= 0) rn customizedNotifyAll(); rn rn else rn rn while(tempCounter > 0) rn try rn System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"will invoke WAIT()"); rn --tempCounter; rn wait(); rn rn rn catch (InterruptedException e) rn e.printStackTrace(); rn notifyAll(); rn rn System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"has been ACTIVED"); rn rn rn customizedNotifyAll(); rn rn rn rn rn rn public void customizedNotifyAll() rn notify(); //可以把这句换成notifyAll(); rn System.out.println(Thread.currentThread().getName()+"-<"+name+counter+">"+"::"+"INVOKED NOTIFYALL() AND FINISHED"); rn rn rn rnrn问题:不是都说notify() 方法是随机唤醒等待的线程吗?但为什么我运行的结果每次都是一样的呢??rn运行结果始终是:rnThread-0-will invoke WAIT()rnThread-1-will invoke WAIT()rnThread-2-will invoke WAIT()rnThread-3-::INVOKED NOTIFYALL() AND FINISHEDrnThread-0-has been ACTIVEDrnThread-0-will invoke WAIT()rnrn还有把public void customizedNotifyAll()方法里的notify()语句换成notifyAll()语句,唤醒所有等待的线程,让这些线程去竞争同步资源,既然是竞争,按理说运行结果也应该是随机的才对啊,为什么我的运行结果每次又都是一样的呢?rn结果如下:rnThread-0-will invoke WAIT()rnThread-1-will invoke WAIT()rnThread-2-will invoke WAIT()rnThread-3-::INVOKED NOTIFYALL() AND FINISHEDrnThread-2-has been ACTIVEDrnThread-2-::INVOKED NOTIFYALL() AND FINISHEDrnThread-1-has been ACTIVEDrnThread-1-will invoke WAIT()rnThread-0-has been ACTIVEDrnThread-0-will invoke WAIT()rnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试