Java并發(fā)編程線程間通訊實(shí)現(xiàn)過(guò)程詳解
在Java中線程間通訊有多種方式,我這里列出一些常用方式,并用代碼的方式展示他們是如何實(shí)現(xiàn)的:
共享變量 wait, notify,notifyAll(這3個(gè)方法是Object對(duì)象中的方法,且必須與synchronized關(guān)鍵字結(jié)合使用) CyclicBarrier、CountDownLatch 利用LockSupport Lock/Condition機(jī)制 管道,創(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 張遠(yuǎn)志 * @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方法會(huì)釋放鎖 } catch (InterruptedException e) { }}System.out.println('任務(wù)A收到通知,繼續(xù)執(zhí)行作業(yè)'); } latch.countDown(); } } class JobB implements Runnable { @Override public void run() { while (flag) { // 保證JobA先申請(qǐng)到鎖 } synchronized (lock) {for (int i = 1; i <= 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { } int a = num.incrementAndGet(); System.out.println('任務(wù)B第' + i + '次執(zhí)行,num值為:' + a); if (a == 3) { lock.notify(); // 喚醒JobB線程,notify方法不會(huì)釋放鎖 }} } latch.countDown(); } } @Test public void test() { latch = new CountDownLatch(2); new Thread(new JobA()).start(); new Thread(new JobB()).start(); try { latch.await(); // 保證2個(gè)線程都執(zhí)行完畢 } catch (InterruptedException e) { } }}
結(jié)果輸出:
任務(wù)B第1次執(zhí)行,num值為:1任務(wù)B第2次執(zhí)行,num值為:2任務(wù)B第3次執(zhí)行,num值為:3任務(wù)B第4次執(zhí)行,num值為:4任務(wù)B第5次執(zhí)行,num值為:5任務(wù)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 張遠(yuǎn)志 * @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('任務(wù)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('任務(wù)B第' + i + '次執(zhí)行,num值為:' + num);if (num == 3) { LockSupport.unpark(ta); // unpark會(huì)立即激活傳入線程} } 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個(gè)線程都執(zhí)行完畢 } catch (InterruptedException e) { } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法2. CSS hack用法案例詳解3. PHP設(shè)計(jì)模式中工廠模式深入詳解4. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息5. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)6. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明7. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析10. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向
