Java并發(fā)編程線程間通訊實現(xiàn)過程詳解
更新時間:2020年05月13日 15:31:40 作者:玄同太子
這篇文章主要介紹了Java并發(fā)編程線程間通訊實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
在Java中線程間通訊有多種方式,我這里列出一些常用方式,并用代碼的方式展示他們是如何實現(xiàn)的:
- 共享變量
- wait, notify,notifyAll(這3個方法是Object對象中的方法,且必須與synchronized關鍵字結合使用)
- CyclicBarrier、CountDownLatch
- 利用LockSupport
- Lock/Condition機制
- 管道,創(chuàng)建管道輸出流PipedOutputStream和管道輸入流PipedInputStream
示例一:
package com.zhi.test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
/**
* Java多線程-線程通訊示例<br>
* flag作為共享變量JobB執(zhí)行,notify通知Job執(zhí)行,CountDownLatch通知主線程執(zhí)行
*
* @author 張遠志
* @since 2020年5月4日21:51:24
*
*/
public class ThreadTest2 {
private CountDownLatch latch;
private volatile boolean flag = true;
private Object lock = new Object();
private AtomicInteger num = new AtomicInteger(0);
class JobA implements Runnable {
@Override
public void run() {
synchronized (lock) {
flag = false;
if (num.get() != 3) {
try {
lock.wait(); // wait方法會釋放鎖
} catch (InterruptedException e) {
}
}
System.out.println("任務A收到通知,繼續(xù)執(zhí)行作業(yè)");
}
latch.countDown();
}
}
class JobB implements Runnable {
@Override
public void run() {
while (flag) { // 保證JobA先申請到鎖
}
synchronized (lock) {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
int a = num.incrementAndGet();
System.out.println("任務B第" + i + "次執(zhí)行,num值為:" + a);
if (a == 3) {
lock.notify(); // 喚醒JobB線程,notify方法不會釋放鎖
}
}
}
latch.countDown();
}
}
@Test
public void test() {
latch = new CountDownLatch(2);
new Thread(new JobA()).start();
new Thread(new JobB()).start();
try {
latch.await(); // 保證2個線程都執(zhí)行完畢
} catch (InterruptedException e) {
}
}
}
結果輸出:
任務B第1次執(zhí)行,num值為:1
任務B第2次執(zhí)行,num值為:2
任務B第3次執(zhí)行,num值為:3
任務B第4次執(zhí)行,num值為:4
任務B第5次執(zhí)行,num值為:5
任務A收到通知,繼續(xù)執(zhí)行作業(yè)
示例二:
package com.zhi.test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;
import org.junit.Test;
/**
* Java多線程-線程通訊示例,利用LockSupport
*
* @author 張遠志
* @since 2020年5月4日21:51:24
*
*/
public class ThreadTest3 {
private CountDownLatch latch;
private volatile int num = 0;
private Thread ta;
private Thread tb;
class JobA implements Runnable {
@Override
public void run() {
if (num != 3) {
LockSupport.park();
}
System.out.println("任務A收到通知,繼續(xù)執(zhí)行作業(yè)");
latch.countDown();
}
}
class JobB implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
num++;
System.out.println("任務B第" + i + "次執(zhí)行,num值為:" + num);
if (num == 3) {
LockSupport.unpark(ta); // unpark會立即激活傳入線程
}
}
latch.countDown();
}
}
@Test
public void test() {
latch = new CountDownLatch(2);
ta = new Thread(new JobA());
tb = new Thread(new JobB());
ta.start();
tb.start();
try {
latch.await(); // 保證2個線程都執(zhí)行完畢
} catch (InterruptedException e) {
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java學生信息管理系統(tǒng)設計(數(shù)據(jù)庫版)
這篇文章主要為大家詳細介紹了數(shù)據(jù)庫版的Java學生信息管理系統(tǒng)設計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
SpringMVC 使用JSR-303進行校驗 @Valid示例
本篇文章主要介紹了SpringMVC 使用JSR-303進行校驗 @Valid示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
spring的TransactionSynchronizationAdapter事務源碼解析
這篇文章主要介紹了spring的TransactionSynchronizationAdapter事務源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09

