日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA多线程-基础Lock Condition 并发集合

發布時間:2024/4/14 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?
001package concurrency;
002?
003import java.util.ArrayList;
004import java.util.concurrent.ArrayBlockingQueue;
005import java.util.concurrent.locks.Condition;
006import java.util.concurrent.locks.ReentrantLock;
007?
008public 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();
014?????
015????private class Negotiation implements Runnable {
016????????private String number;
017????????private Negotiation(String number) {
018????????????this.number = number;
019????????}
020????????public void run() {
021????????????try {
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();
028????????????????????}
029????????????????????System.out.println("Negotiation broken post handle.");
030????????????????????return;
031????????????????}
032????????????????System.out.println("Negotiation succeeds.");
033????????????} catch (InterruptedException e) {
034????????????????System.out.println("Middle man is killed.");
035????????????}
036????????????negotiationLock.lock();
037????????????negotiating = false;
038????????????negotiationCondition.signalAll();
039????????????negotiationLock.unlock();
040????????}
041????}
042?????
043????private class Carrier implements Runnable {
044????????private String name;
045????????private Carrier(String name) {
046????????????this.name = name;
047????????}
048????????public void run() {
049????????????while(true) {
050????????????????try{
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;
059????????????????????}
060????????????????}
061????????????????} finally {
062????????????????negotiationLock.unlock();
063????????????????}
064????????????????String number;
065????????????????try {
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;
071????????????????}
072??????????????????
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????????????????????????}
083????????????????????}
084????????????????????negotiating = true;
085????????????????????System.out.println("Stricks happen on number:"+number);
086????????????????????new Thread(new Negotiation(number)).start();
087????????????????????} finally {
088????????????????????negotiationLock.unlock();
089????????????????????}
090????????????????}
091????????????}
092????????}
093????}
094?????
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);
103?????????
104????????a.start();
105????????b.start();
106????????c.start();
107?????????
108????????this.produceNumbers();
109?????????
110????}
111?????
112????private void produceNumbers() {
113????????while (true) {
114????????????while(negotiating) {
115????????????????negotiationLock.lock();
116????????????????try {
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();
125????????????????}
126????????????}
127?????????????
128????????????String number = ""+new java.util.Random().nextInt(47);
129???????????
130????????????try {
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;
136????????????}
137????????}
138????}
139?????????
140?????
141????public static void main(String[] args) {
142????????new Producer().start();
143????}
144}

轉載于:https://www.cnblogs.com/shihao/archive/2012/11/09/2762221.html

總結

以上是生活随笔為你收集整理的JAVA多线程-基础Lock Condition 并发集合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。