Java通過(guò)wait()和notifyAll()方法實(shí)現(xiàn)線程間通信
本文實(shí)例為大家分享了Java實(shí)現(xiàn)線程間通信的具體代碼,供大家參考,具體內(nèi)容如下
Java代碼(使用了2個(gè)內(nèi)部類):
package Threads;
import java.util.LinkedList;
/**
* Created by Frank
*/
public class ProdCons {
protected LinkedList<Object> list = new LinkedList<>();
protected int max;
protected boolean done = false;
public static void main(String[] args) throws InterruptedException {
ProdCons prodCons = new ProdCons(100, 3, 4);
Thread.sleep(5 * 1000);
synchronized (prodCons.list) {
prodCons.done = true;
try {
prodCons.notifyAll();
} catch (Exception ex) {
}
}
}
private ProdCons(int maxThreads, int nP, int nC) {
this.max = maxThreads;
for (int i = 0; i < nP; i++) {
new Producer().start();
}
for (int i = 0; i < nC; i++) {
new Consumer().start();
}
}
class Producer extends Thread {
public void run() {
while (true) {
Object justProduced = null;
try {
justProduced = getObj();
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (list) {
while (list.size() == max) {
try {
list.wait();
} catch (InterruptedException e) {
System.out.println("Producer INTERRUPTED");
}
}
list.addFirst(justProduced);
list.notifyAll();
System.out.println("Produced 1;List size now " + list.size());
if (done) {
break;
}
}
}
}
}
class Consumer extends Thread {
public void run() {
while (true) {
Object object = null;
synchronized (list) {
if (list.size() == 0) {
try {
list.wait();
} catch (InterruptedException e) {
System.out.println("Consumer INTERRUPTED");
}
}
if (list.size() > 0) {
object = list.removeLast();
}
list.notifyAll();
System.out.println("List size now " + list.size());
if (done) {
break;
}
}
if (null != object) {
System.out.println("Consuming object " + object);
}
}
}
}
private Object getObj() throws InterruptedException {
Thread.sleep(1000);
return new Object();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring aop之@AfterReturning不生效問(wèn)題及解決
這篇文章主要介紹了spring aop之@AfterReturning不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringMVC文件上傳原理及實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了SpringMVC文件上傳原理及實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
mybatis的動(dòng)態(tài)SQL和模糊查詢實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于mybatis的動(dòng)態(tài)SQL和模糊查詢的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Kotlin語(yǔ)言編程Regex正則表達(dá)式實(shí)例詳解
這篇文章主要為大家介紹了Kotlin語(yǔ)言編程Regex正則表達(dá)式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Java如何使用流去除集合中某個(gè)字段為空的對(duì)象
這篇文章主要給大家介紹了關(guān)于Java如何使用流去除集合中某個(gè)字段為空的對(duì)象,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-08-08
Spring?Boot?使用?SSE?方式向前端推送數(shù)據(jù)詳解
這篇文章主要介紹了Spring?Boot?使用SSE方式向前端推送數(shù)據(jù)詳解,SSE簡(jiǎn)單的來(lái)說(shuō)就是服務(wù)器主動(dòng)向前端推送數(shù)據(jù)的一種技術(shù),它是單向的,也就是說(shuō)前端是不能向服務(wù)器發(fā)送數(shù)據(jù)的2022-08-08

