signalAll() 和signal() 區別java
signalAll() 會喚醒全部的正在等待的線程ide
可是signal是公平的,只會喚起那個等待時間最長的線程spa
package com.yxl.task;
import lombok.SneakyThrows;import org.omg.PortableInterceptor.INACTIVE;
import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.concurrent.*;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;
/** * Condition 演示demo */public class ConditionDemo {
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
//方法1 void method1(){ lock.lock(); try { System.out.println("條件不知足,開始await"); condition.await(); System.out.println("條件知足了,開始執行後續");
} catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }
//方法2 void method2() { lock.lock(); try { System.out.println("準備工做完成,喚醒其餘線程 "); condition.signal(); } finally { lock.unlock(); } }
public static void main(String[] args) { ConditionDemo conditionDemo =new ConditionDemo(); new Thread(new Runnable() { @SneakyThrows @Override public void run() { Thread.sleep(1000); conditionDemo.method2(); } }).start(); conditionDemo.method1(); }}
執行結果:線程
條件不知足,開始await 準備工做完成,喚醒其餘線程 條件知足了,開始執行後續3d
package com.yxl.task;在這裏插入圖片描述
import lombok.SneakyThrows;import org.omg.PortableInterceptor.INACTIVE;import org.omg.PortableServer.THREAD_POLICY_ID;
import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.PriorityQueue;import java.util.concurrent.*;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;
/** * Condition 實現生產者 消費者 */public class ConditionDemo {
private int queueSize = 10 ; //隊列 private PriorityQueue queue =new PriorityQueue(queueSize); //lock鎖 private ReentrantLock lock = new ReentrantLock();
private Condition notFull = lock.newCondition(); private Condition notEmpty = lock.newCondition();
//方法1 class Consumer extends Thread { @SneakyThrows @Override public void run() { comsume(); } private void comsume() throws InterruptedException { while (true){ lock.lock(); try { while (queue.size() == 0) { System.out.println("隊列空,等待數據"); notEmpty.await(); } queue.poll(); notFull.signalAll(); System.out.println("從隊列裏驅走了一個數據,剩下"+ queue.size()); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); }
}
} }
class Produce extends Thread { @SneakyThrows @Override public void run() { produce(); } private void produce() throws InterruptedException { while (true){ lock.lock(); try { while (queue.size() == queueSize) { System.out.println("隊列滿,等待空閒"); notFull.await(); } queue.offer(1); notEmpty.signalAll(); System.out.println("向隊列插入一個元素"+ queue.size()); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); }
}
} }
public static void main(String[] args) { ConditionDemo conditionDemo =new ConditionDemo(); Produce produce = conditionDemo.new Produce(); Consumer consumer = conditionDemo.new Consumer(); consumer.start(); produce.start(); }}
#有興趣的同窗能夠看下源碼實現 和 JDK 介紹code