22.線程通訊Condition

Condition

    經過此對象能夠與lock實現組合使用,爲每一個對象提供多個等待,實現多個阻塞隊列。
 
 
 
 
 
   
   
   
   
   

多Condition組合

    
    
    
    
    
  1. }

經常使用方法

package dmeo9;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class DemoLock2 { private Lock lock = new ReentrantLock(); private Condition condition1 = lock.newCondition(); public void m1(){ try { lock.lock(); System.err.println(Thread.currentThread().getName()+":線程,進入m1"); Thread.sleep(3000); System.err.println(Thread.currentThread().getName()+":線程,釋放鎖,接收通喚醒知"); condition1.await(); System.err.println(Thread.currentThread().getName()+":線程,繼續執行 m1"); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } public void m2(){ try { lock.lock(); System.err.println(Thread.currentThread().getName()+":線程,進入m2"); Thread.sleep(3000); System.err.println(Thread.currentThread().getName()+":線程,發出通知,喚醒"); condition1.signal(); System.err.println(Thread.currentThread().getName()+":線程,繼續執行 m2"); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } public static void main(String[] atgs) { final DemoLock2 demoLock = new DemoLock2(); Thread t1 = new Thread(new Runnable() { @Override public void run() { demoLock.m1();; } },"消費者t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { demoLock.m2();; } },"生產者t2"); t1.start(); t2.start(); }}輸出:消費者t1:線程,進入m1 消費者t1:線程,釋放鎖,接收通喚醒知 生產者t2:線程,進入m2 生產者t2:線程,發出通知,喚醒 生產者t2:線程,繼續執行 m2 消費者t1:線程,繼續執行 m1
package dmeo9;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * Created by liudan on 2017/8/4. */public class DemoLock3 { /** * 經過一對象產生lock對象,並生產多個Condition進行多線程的交互,能夠作到一部分喚醒,另外一部分繼續等待。 */ private Lock lock = new ReentrantLock(); private Condition condition1 = lock.newCondition(); private Condition condition2 = lock.newCondition(); public void m1() { try { lock.lock(); System.err.println(Thread.currentThread().getName() + ":進入m1"); Thread.sleep(100); System.err.println(Thread.currentThread().getName() + ":m1釋放鎖,接收通喚醒知"); condition1.await(); System.err.println(Thread.currentThread().getName() + ":繼續執行 m1"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m2() { try { lock.lock(); System.err.println(Thread.currentThread().getName() + ":進入m2"); Thread.sleep(200); System.err.println(Thread.currentThread().getName() + ":m2釋放鎖,接收通喚醒知"); condition1.await(); System.err.println(Thread.currentThread().getName() + ":繼續執行 m2"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m3() { try { lock.lock(); System.err.println(Thread.currentThread().getName() + ":進入m3"); Thread.sleep(300); System.err.println(Thread.currentThread().getName() + ":m3釋放鎖,接收通喚醒知"); condition2.await(); System.err.println(Thread.currentThread().getName() + ":繼續執行 m3"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m4() { try { lock.lock(); System.err.println(Thread.currentThread().getName() + ":進入m4"); Thread.sleep(400); System.err.println(Thread.currentThread().getName() + ":m4發出通知並喚醒:m一、m2"); condition1.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m5() { try { lock.lock(); System.err.println(Thread.currentThread().getName() + ":進入m5"); Thread.sleep(500); System.err.println(Thread.currentThread().getName() + ":m5發出通知並喚醒:m3"); condition2.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] atgs) { DemoLock3 demoLock3 = new DemoLock3(); Thread t1 = new Thread(new Runnable() { @Override public void run() { demoLock3.m1(); } }, "_1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { demoLock3.m2(); } }, "_2"); Thread t3 = new Thread(new Runnable() { @Override public void run() { demoLock3.m3(); } }, "_3"); Thread t4 = new Thread(new Runnable() { @Override public void run() { demoLock3.m4(); } }, "_4"); Thread t5 = new Thread(new Runnable() { @Override public void run() { demoLock3.m5(); } }, "_5"); t1.start(); t2.start(); t3.start(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } t4.start(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } t5.start();}輸出:_1:進入m1 _1:m1釋放鎖,接收通喚醒知 _2:進入m2 _2:m2釋放鎖,接收通喚醒知 _3:進入m3 _3:m3釋放鎖,接收通喚醒知 _4:進入m4 _4:m4發出通知並喚醒:m一、m2 _1:繼續執行 m1 _2:繼續執行 m2 _5:進入m5 _5:m5發出通知並喚醒:m3 _3:繼續執行 m3
* 公平鎖、非公平鎖 * Lock lock = new ReentrantLock(boolean isFair)。 * tryLock()嘗試去獲取鎖,並得到結果用false、true返回,並在給定的時間內去獲取。 * isFair():是否公平鎖。 * isLocked():是否鎖定 * getHoldCount():查詢當前的線程保存的鎖存在的個數,也就是調用lock的次數。 * lockinterruptibly():優先響應中斷的鎖。 * getQueueLength():返回正在等待獲取此鎖的線程數量。 * getWaitQueueLenth():獲取返回等待與鎖定相關的給定條件condition的線程數量。 * hasQueuedThread(Thread thread):查詢指定的線程是否正在等待此鎖。 * hasQueuedThread():查詢是否有線程正在等待次鎖。 * hasWaiters():查詢是否有線程正在等待與此鎖有關的condition條件。
相關文章
相關標籤/搜索