生活随笔
收集整理的這篇文章主要介紹了
JAVA多线程-基础Lock Condition 并发集合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼的邏輯:
1)SProducer不停的產生number到queue中.
2)3個carrier不停的取出queue中的number.
3)如果queue中存在10個剩余number時,SProducer會停下來等Carrier消費一些后再生產.
4)如果Carrier發現queue中沒有number時,會等待.
5)如果Carrier取出的數字末尾為4, 則會挑起罷工事件.
6)Carrier罷工會引發一個Negotiation線程進行談判.
7)罷工階段SProducer和所有Carrier均停工.
8)Negotiation如果發現取出的number首位為3或者7,將引發談判失敗.
9)如果談判成功,則恢復工作,如果談判失敗,則破產,所有線程均退出.
注意:使用lock的時候一定要注意, lock()和unlock()方法一定要成對出現. 最好放到try{}finally{}中,這樣,unlock()方法必會調到.倘若沒有使用unlock()就return了,會導致線程死鎖.
view sourceprint?
| 003 | import java.util.ArrayList; |
| 004 | import java.util.concurrent.ArrayBlockingQueue; |
| 005 | import java.util.concurrent.locks.Condition; |
| 006 | import java.util.concurrent.locks.ReentrantLock; |
| 008 | public class Producer extends Thread { |
| 009 | ????private final static ArrayBlockingQueue<String> numbers = new ArrayBlockingQueue<String>(10); |
| 010 | ????private final static ArrayList<Thread> threads = new ArrayList<Thread>(); |
| 011 | ????private volatile boolean negotiating? = false; |
| 012 | ????private ReentrantLock negotiationLock = new ReentrantLock(); |
| 013 | ????private Condition negotiationCondition = negotiationLock.newCondition(); |
| 015 | ????private class Negotiation implements Runnable { |
| 016 | ????????private String number; |
| 017 | ????????private Negotiation(String number) { |
| 018 | ????????????this.number = number; |
| 020 | ????????public void run() { |
| 022 | ????????????????System.out.println("Start negotiation..."); |
| 023 | ????????????????sleep(5000); |
| 024 | ????????????????if (number.startsWith("7") || number.startsWith("3")) { |
| 025 | ????????????????????System.out.println("Negotiation broken."); |
| 026 | ????????????????????for (Thread t : threads) { |
| 027 | ????????????????????????t.interrupt(); |
| 029 | ????????????????????System.out.println("Negotiation broken post handle."); |
| 030 | ????????????????????return; |
| 032 | ????????????????System.out.println("Negotiation succeeds."); |
| 033 | ????????????} catch (InterruptedException e) { |
| 034 | ????????????????System.out.println("Middle man is killed."); |
| 036 | ????????????negotiationLock.lock(); |
| 037 | ????????????negotiating = false; |
| 038 | ????????????negotiationCondition.signalAll(); |
| 039 | ????????????negotiationLock.unlock(); |
| 043 | ????private class Carrier implements Runnable { |
| 044 | ????????private String name; |
| 045 | ????????private Carrier(String name) { |
| 046 | ????????????this.name = name; |
| 048 | ????????public void run() { |
| 049 | ????????????while(true) { |
| 051 | ????????????????negotiationLock.lock(); |
| 052 | ????????????????while(negotiating) { |
| 053 | ????????????????????try { |
| 054 | ????????????????????????System.out.println("Carrier ["+name+"] join stricks."); |
| 055 | ????????????????????????negotiationCondition.await(); |
| 056 | ????????????????????} catch (InterruptedException e) { |
| 057 | ????????????????????????System.out.println("Negotiation fails. Carrier [" + name + "] retires."); |
| 058 | ????????????????????????return; |
| 061 | ????????????????} finally { |
| 062 | ????????????????negotiationLock.unlock(); |
| 064 | ????????????????String number; |
| 066 | ????????????????????number = numbers.take(); |
| 067 | ????????????????????System.out.println("Carrier [" + name + "] carries "+ number +" out of List;"); |
| 068 | ????????????????} catch (InterruptedException e1) { |
| 069 | ?????????????????????System.out.println("Negotiation fails. Carrier [" + name + "] retires."); |
| 070 | ?????????????????????return; |
| 073 | ????????????????if (number.endsWith("4")) { |
| 074 | ????????????????????try { |
| 075 | ????????????????????negotiationLock.lock(); |
| 076 | ????????????????????while (negotiating) { |
| 077 | ????????????????????????try { |
| 078 | ????????????????????????????negotiationCondition.await(); |
| 079 | ????????????????????????} catch (InterruptedException e) { |
| 080 | ????????????????????????????System.out.println("Negotiation fails. Carrier [" + name + "] retires."); |
| 081 | ????????????????????????????return; |
| 082 | ????????????????????????} |
| 084 | ????????????????????negotiating = true; |
| 085 | ????????????????????System.out.println("Stricks happen on number:"+number); |
| 086 | ????????????????????new Thread(new Negotiation(number)).start(); |
| 087 | ????????????????????} finally { |
| 088 | ????????????????????negotiationLock.unlock(); |
| 095 | ????public void run() { |
| 096 | ????????Thread a = new Thread(new Carrier("a")); |
| 097 | ????????Thread b = new Thread(new Carrier("b")); |
| 098 | ????????Thread c = new Thread(new Carrier("c")); |
| 099 | ????????threads.add(this); |
| 100 | ????????threads.add(a); |
| 101 | ????????threads.add(b); |
| 102 | ????????threads.add(c); |
| 108 | ????????this.produceNumbers(); |
| 112 | ????private void produceNumbers() { |
| 113 | ????????while (true) { |
| 114 | ????????????while(negotiating) { |
| 115 | ????????????????negotiationLock.lock(); |
| 117 | ????????????????????System.out.println("Stricking... Producer waiting for negotiation result."); |
| 118 | ????????????????????negotiationCondition.await(); |
| 119 | ????????????????????System.out.println("Negotiation succeeds. Producer happy."); |
| 120 | ????????????????} catch (InterruptedException e) { |
| 121 | ????????????????????System.out.println("Negotiation fails. Producer breaks up."); |
| 122 | ????????????????????return; |
| 123 | ????????????????} finally { |
| 124 | ????????????????????negotiationLock.unlock(); |
| 128 | ????????????String number = ""+new java.util.Random().nextInt(47); |
| 131 | ????????????????numbers.put(number); |
| 132 | ????????????????System.out.println("Produce number " + number + " into List;"); |
| 133 | ????????????} catch (InterruptedException e) { |
| 134 | ????????????????System.out.println("Negotiation fails. Producer breaks up."); |
| 135 | ????????????????return; |
| 141 | ????public static void main(String[] args) { |
| 142 | ????????new Producer().start(); |
轉載于:https://www.cnblogs.com/shihao/archive/2012/11/09/2762221.html
總結
以上是生活随笔為你收集整理的JAVA多线程-基础Lock Condition 并发集合的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。