Java騷操作之CountDownLatch代碼詳解
用來(lái)干嘛的?當(dāng)你在方法中調(diào)用了多個(gè)線程,對(duì)數(shù)據(jù)庫(kù)進(jìn)行了一些不為人知的操作后,還有一個(gè)操作需要留到前者都執(zhí)行完的重頭戲,就需要用到 CountDownLatch 了
實(shí)踐代碼package com.github.gleans;import java.util.concurrent.CountDownLatch;public class TestCountDownLatch { public static void main(String[] args) throws InterruptedException { CountDownLatch latch = new CountDownLatch(3); new KeyPass(1000L, 'thin jack', latch).start(); new KeyPass(2000L, 'noral jack', latch).start(); new KeyPass(3000L, 'fat jack', latch).start(); latch.await(); System.out.println('此處對(duì)數(shù)據(jù)庫(kù)進(jìn)行最后的插入操作~'); } static class KeyPass extends Thread { private long times; private CountDownLatch countDownLatch; public KeyPass(long times, String name, CountDownLatch countDownLatch) { super(name); this.times = times; this.countDownLatch = countDownLatch; } @Override public void run() { try {System.out.println('操作人:' + Thread.currentThread().getName() + '對(duì)數(shù)據(jù)庫(kù)進(jìn)行插入,持續(xù)時(shí)間:' + this.times / 1000 + '秒');Thread.sleep(times);countDownLatch.countDown(); } catch (InterruptedException e) {e.printStackTrace(); } } }}圖解
使用await()提前結(jié)束操作
package com.github.gleans;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;public class TestCountDownLatch { public static void main(String[] args) throws InterruptedException { CountDownLatch latch = new CountDownLatch(3); new KeyPass(2000L, '公司一', latch).start(); new KeyPass(3000L, '公司二', latch).start(); new KeyPass(5000L, '公司三', latch).start(); latch.await(2, TimeUnit.SECONDS); System.out.println('~~~賈總PPT巡演~~~~'); System.out.println('~~~~融資完成,撒花~~~~'); } static class KeyPass extends Thread { private long times; private CountDownLatch countDownLatch; public KeyPass(long times, String name, CountDownLatch countDownLatch) { super(name); this.times = times; this.countDownLatch = countDownLatch; } @Override public void run() { try {Thread.sleep(times);System.out.println('負(fù)責(zé)人:' + Thread.currentThread().getName() + '開始工作,持續(xù)時(shí)間:' + this.times / 1000 + '秒');countDownLatch.countDown(); } catch (InterruptedException e) {e.printStackTrace(); } } }}
假設(shè)公司一、公司二、公司三各需要2s、3s、5s來(lái)完成工作,賈總等不了,只能等2s,那么就設(shè)置await的超時(shí)時(shí)間
latch.await(2, TimeUnit.SECONDS);執(zhí)行結(jié)果
負(fù)責(zé)人:公司一開始工作,持續(xù)時(shí)間:2秒~~~賈總PPT巡演~~~~~~~~融資完成,撒花~~~~負(fù)責(zé)人:公司二開始工作,持續(xù)時(shí)間:3秒負(fù)責(zé)人:公司三開始工作,持續(xù)時(shí)間:5秒
方法描述
這個(gè)操作可以說(shuō)是簡(jiǎn)單好用,目前還未遇見副作用,若是有大佬,可以告知弟弟一下,提前表示感謝~
到此這篇關(guān)于Java騷操作之CountDownLatch的文章就介紹到這了,更多相關(guān)Java CountDownLatch內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過(guò)程和條件語(yǔ)句)2. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)3. jscript與vbscript 操作XML元素屬性的代碼4. Jsp servlet驗(yàn)證碼工具類分享5. XML在語(yǔ)音合成中的應(yīng)用6. 基于PHP做個(gè)圖片防盜鏈7. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)8. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)9. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)10. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
