Java多線程通信實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Java多線程通信實(shí)現(xiàn)方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
線程通信的方式:
1、共享變量
線程間通信可以通過發(fā)送信號(hào),發(fā)送信號(hào)的一個(gè)簡(jiǎn)單方式是在共享對(duì)象的變量里設(shè)置信號(hào)值。線程A在一個(gè)同步塊里設(shè)置boolean型成員變量hasDataToProcess為true,線程B也在同步代碼塊里讀取hasDataToProcess這個(gè)成員變量。這個(gè)簡(jiǎn)單的例子使用了一個(gè)持有信號(hào)的對(duì)象,并提供了set和get方法。
public class MySignal1 {
//共享的變量
private boolean hasDataToProcess = false;
//取值
public boolean getHasDataProcess() {
return hasDataToProcess;
}
//存值
public void setHasDataToProcess(boolean hasDataToProcess) {
this.hasDataToProcess = hasDataToProcess;
}
public static void main(String[] args) {
//同一個(gè)對(duì)象
final MySignal1 my = new MySignal1();
//線程1設(shè)置hasDataToProcess值為true
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
my.setHasDataToProcess(true);
}
});
t1.start();
//線程2取這個(gè)值hasDataToProcess
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
//等待線程1完成后取值
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
my.getHasDataProcess();
System.out.println("t1改變以后的值:"+my.getHasDataProcess());
}
});
t2.start();
}
}
運(yùn)行結(jié)果如下:
t1改變以后的值:true
2、等待/喚醒(wait/notify)機(jī)制
以資源為例,生產(chǎn)者生產(chǎn)一個(gè)資源,通知消費(fèi)者就消費(fèi)掉一個(gè)資源,生產(chǎn)者繼續(xù)生產(chǎn)資源,消費(fèi)者消費(fèi)資源,以此循環(huán),代碼如下。
import sun.security.util.Password;
//資源類
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name) {
//生產(chǎn)資源
while (flag) {
try {
//線程等待
wait();
} catch (InterruptedException e) {
}
}
this.name = name + "----" + count + "+++++";
System.out.println(Thread.currentThread().getName() + "..生產(chǎn)者..." + this.name);
flag = true;
//喚醒等待中的消費(fèi)者
this.notifyAll();
}
public synchronized void out() {
//消費(fèi)資源
while (!flag) {
try {
//線程等待,生產(chǎn)者生產(chǎn)資源
wait();
} catch (InterruptedException e) {
}
}
System.out.println(Thread.currentThread().getName() + "...消費(fèi)者..." + this.name);
flag = false;
//喚醒消費(fèi)者,生產(chǎn)資源
this.notifyAll();
}
}
//生產(chǎn)者
class Producer implements Runnable {
private Resource rs;
public Producer(Resource rs) {
this.rs = rs;
}
//生產(chǎn)者生產(chǎn)資源
@Override
public void run() {
while (true) {
rs.set("商品");
}
}
}
//消費(fèi)者消費(fèi)資源
class Consumer implements Runnable {
private Resource rs;
public Consumer(Resource rs) {
this.rs = rs;
}
//消費(fèi)者消費(fèi)資源
@Override
public void run() {
while (true) {
rs.out();
}
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource r = new Resource();
Producer p = new Producer(r);
Consumer c = new Consumer(r);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
運(yùn)行結(jié)果如下:
Thread-0..生產(chǎn)者...商品----1+++++ Thread-1...消費(fèi)者...商品----1+++++ Thread-0..生產(chǎn)者...商品----1+++++ Thread-1...消費(fèi)者...商品----1+++++ Thread-0..生產(chǎn)者...商品----1+++++ Thread-1...消費(fèi)者...商品----1+++++ Thread-0..生產(chǎn)者...商品----1+++++
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)手寫線程池實(shí)例并測(cè)試詳解
這篇文章主要來模擬一下線程池和工作隊(duì)列的流程,以及編寫代碼和測(cè)試類進(jìn)行測(cè)試。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02
Java初級(jí)必看的數(shù)據(jù)類型與常量變量知識(shí)點(diǎn)
這篇文章主要給大家介紹了關(guān)于Java初級(jí)必看的數(shù)據(jù)類型與常量變量知識(shí)點(diǎn)的相關(guān)資料,需要的朋友可以參考下2023-11-11
Java探索之Hibernate主鍵生成策略詳細(xì)介紹
這篇文章主要介紹了Java探索之Hibernate主鍵生成策略詳細(xì)介紹,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn)
這篇文章主要介紹了Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn),文中通過示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
淺談maven 多環(huán)境打包發(fā)布的兩種方式
這篇文章主要介紹了淺談maven 多環(huán)境打包發(fā)布的兩種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
基于springboot實(shí)現(xiàn)redis分布式鎖的方法
這篇文章主要介紹了基于springboot實(shí)現(xiàn)redis分布式鎖的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

