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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/8 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lock 线程 java_JAVA多线程-基础Lock Condition 并发集合 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

跟上一篇文章比較,這次改進(jìn)了之前的代碼,使用了Lock Condition 和并發(fā)集合.代碼量減了一些,并且更加容易讀了.

這篇代碼是上一篇的改進(jìn)版,邏輯在前篇有說明,以防大家看不到,我再重現(xiàn)貼一遍.后續(xù)會(huì)使用高階的線程工具再次改進(jìn),以求代碼更簡單.代碼的邏輯:

1)SProducer不停的產(chǎn)生number到queue中.

2)3個(gè)carrier不停的取出queue中的number.

3)如果queue中存在10個(gè)剩余number時(shí),SProducer會(huì)停下來等Carrier消費(fèi)一些后再生產(chǎn).

4)如果Carrier發(fā)現(xiàn)queue中沒有number時(shí),會(huì)等待.

5)如果Carrier取出的數(shù)字末尾為4, 則會(huì)挑起罷工事件.

6)Carrier罷工會(huì)引發(fā)一個(gè)Negotiation線程進(jìn)行談判.

7)罷工階段SProducer和所有Carrier均停工.

8)Negotiation如果發(fā)現(xiàn)取出的number首位為3或者7,將引發(fā)談判失敗.

9)如果談判成功,則恢復(fù)工作,如果談判失敗,則破產(chǎn),所有線程均退出.

注意:使用lock的時(shí)候一定要注意, lock()和unlock()方法一定要成對(duì)出現(xiàn). 最好放到try{}finally{}中,這樣,unlock()方法必會(huì)調(diào)到.倘若沒有使用unlock()就return了,會(huì)導(dǎo)致線程死鎖.

package concurrency;

import java.util.ArrayList;

import java.util.concurrent.ArrayBlockingQueue;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.ReentrantLock;

public class Producer extends Thread {

private final static ArrayBlockingQueue numbers = new ArrayBlockingQueue(10);

private final static ArrayList threads = new ArrayList();

private volatile boolean negotiating = false;

private ReentrantLock negotiationLock = new ReentrantLock();

private Condition negotiationCondition = negotiationLock.newCondition();

private class Negotiation implements Runnable {

private String number;

private Negotiation(String number) {

this.number = number;

}

public void run() {

try {

System.out.println("Start negotiation...");

sleep(5000);

if (number.startsWith("7") || number.startsWith("3")) {

System.out.println("Negotiation broken.");

for (Thread t : threads) {

t.interrupt();

}

System.out.println("Negotiation broken post handle.");

return;

}

System.out.println("Negotiation succeeds.");

} catch (InterruptedException e) {

System.out.println("Middle man is killed.");

}

negotiationLock.lock();

negotiating = false;

negotiationCondition.signalAll();

negotiationLock.unlock();

}

}

private class Carrier implements Runnable {

private String name;

private Carrier(String name) {

this.name = name;

}

public void run() {

while(true) {

try{

negotiationLock.lock();

while(negotiating) {

try {

System.out.println("Carrier ["+name+"] join stricks.");

negotiationCondition.await();

} catch (InterruptedException e) {

System.out.println("Negotiation fails. Carrier [" + name + "] retires.");

return;

}

}

} finally {

negotiationLock.unlock();

}

String number;

try {

number = numbers.take();

System.out.println("Carrier [" + name + "] carries "+ number +" out of List;");

} catch (InterruptedException e1) {

System.out.println("Negotiation fails. Carrier [" + name + "] retires.");

return;

}

if (number.endsWith("4")) {

try {

negotiationLock.lock();

while (negotiating) {

try {

negotiationCondition.await();

} catch (InterruptedException e) {

System.out.println("Negotiation fails. Carrier [" + name + "] retires.");

return;

}

}

negotiating = true;

System.out.println("Stricks happen on number:"+number);

new Thread(new Negotiation(number)).start();

} finally {

negotiationLock.unlock();

}

}

}

}

}

public void run() {

Thread a = new Thread(new Carrier("a"));

Thread b = new Thread(new Carrier("b"));

Thread c = new Thread(new Carrier("c"));

threads.add(this);

threads.add(a);

threads.add(b);

threads.add(c);

a.start();

b.start();

c.start();

this.produceNumbers();

}

private void produceNumbers() {

while (true) {

while(negotiating) {

negotiationLock.lock();

try {

System.out.println("Stricking... Producer waiting for negotiation result.");

negotiationCondition.await();

System.out.println("Negotiation succeeds. Producer happy.");

} catch (InterruptedException e) {

System.out.println("Negotiation fails. Producer breaks up.");

return;

} finally {

negotiationLock.unlock();

}

}

String number = ""+new java.util.Random().nextInt(47);

try {

numbers.put(number);

System.out.println("Produce number " + number + " into List;");

} catch (InterruptedException e) {

System.out.println("Negotiation fails. Producer breaks up.");

return;

}

}

}

public static void main(String[] args) {

new Producer().start();

}

}

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。