freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

[計(jì)算機(jī)]實(shí)戰(zhàn)javaconcurrent(參考版)

2024-09-03 16:03本頁面
  

【正文】 自打有了“輪子”理論,重復(fù)造大輪子的情況的確少了,但還是有人會(huì)做些小輪子,例如編寫多線程程序時(shí)用到的小工具(線程池、鎖等等),如果可以,請(qǐng)讓自己再 “懶惰”一點(diǎn)吧~。 public void run() { } (實(shí)際執(zhí)行時(shí)間: + (stop start) + ms)。 (實(shí)際執(zhí)行驗(yàn)證次數(shù): + ())。 (1000)。 stop = ()。 } catch (InterruptedException e) { ()。 } (())。 public static void main(String[] args) { } = signal。 = validationService。 = entries。 public ValidationStarter(ListString entries, ValidationService validationService, signal。 private ListString* author DigitalSonic*/**import 。import 。import 。} } (entries)。 run() { Override ms )。 (實(shí)際執(zhí)行時(shí)間: (實(shí)際執(zhí)行驗(yàn)證次數(shù): stop = ()。 ()。 (InterruptedException e) { ()。 { (new (new (new } for stop。 start。 CountDownLatch( 3 )。 ArrayListString()。 ValidationService(threadPoolService)。 ThreadPoolService( 10 )。 void static public * 線程池大小為10,初始化執(zhí)行一次,隨后并發(fā)三個(gè)驗(yàn)證 } this .validationService = validationService。 public signal。 private private ListString implements classpublic*/* author DigitalSonic 。import 。 。import 。 。關(guān)于時(shí)間,如果是順序執(zhí)行,52次驗(yàn)證每次等待500ms,那么驗(yàn)證所耗費(fèi)的時(shí)間應(yīng)該是26000ms,使用了多線程后的實(shí)際耗時(shí)遠(yuǎn)小于該數(shù)字(最近一次執(zhí)行耗時(shí)4031ms)。系統(tǒng)運(yùn)行完畢會(huì)顯示實(shí)際執(zhí)行的節(jié)點(diǎn)驗(yàn)證次數(shù)和執(zhí)行時(shí)間。最后將所有這些類整合起來,運(yùn)行環(huán)境檢查的主類如下。CountDownLatch的計(jì)數(shù)器不能重置,所以說它是“一次性”的,如果需要重置計(jì)數(shù)器,可以使用CyclicBarrier。Node0 [Node1, Node2]Node1 [Node3, Node4]Node2 [Node5]Node6 [Node7, Node8]Node7 [Node5, Node9]Node8 [Node3, Node4]CountDownLatchCountDownLatch是一個(gè)一次性的同步輔助工具,允許一個(gè)或多個(gè)線程一直等待,直到計(jì)數(shù)器值變?yōu)?。每次調(diào)用會(huì)模擬遠(yuǎn)程訪問,等待500ms。 public static Node cloneNode(Node originalNode) { } return ()。 */ * 獲得計(jì)數(shù)器的值/** } ()。 try { ()。 (驗(yàn)證節(jié)點(diǎn) + () + [ + () + ])。 Node node = cloneNode((wsdl))。 */ * 模擬執(zhí)行遠(yuǎn)程驗(yàn)證返回節(jié)點(diǎn),每次調(diào)用等待500ms /** //入口1 //入口0 static { private static Logger = new AtomicInteger(0)。 ENTRIES public static final ListNode* author DigitalSonic*/**import 。import 。import 。package 。 } return (())。 (())。 Node newNode = new Node cloneNode(Node originalNode) { */ /** ()。 int static public * 獲得計(jì)數(shù)器的值 } return } } catch try ] )。 [ (驗(yàn)證節(jié)點(diǎn) static public * 模擬執(zhí)行遠(yuǎn)程驗(yàn)證返回節(jié)點(diǎn),每次調(diào)用等待500ms } (node0)。 ((), node9)。 ((), node7)。 ((), node5)。 ((), node3)。 ((), node1)。 (new (new (new (new (new (new ://node9/check?wsdl )。 Node node9 = new Node( NODE8 , ://node7/check?wsdl )。 Node node7 = new ://node6/check?wsdl )。 Node node6 = new Node( NODE5 , ://node4/check?wsdl )。 Node node4 = new Node( NODE3 , ://node2/check?wsdl )。 Node node2 = new Node( NODE1 , //入口 0 Node( NODE0 , static * 構(gòu)造模擬數(shù)據(jù) Logger AtomicInteger( 0 )。 = static private HashMapString, Node()。 MapString, Node NODE_MAP = ArrayListNode()。 = final static public MockNodeValidator {** 模擬執(zhí)行節(jié)點(diǎn)驗(yàn)證的Mock類/**importimport 。import 。import 。 。下面的例子中用AtomicInteger保存全局驗(yàn)證次數(shù)(第69行做了自增的操作),因?yàn)関alidateNode()方法會(huì)同時(shí)被多個(gè)線程調(diào)用,所以直接用int不同步是不行的,但用AtomicInteger在這種場(chǎng)合下就很合適。好在 ,可以以原子方式更新變量。(new ValidationTask(wsdl))。ListValidationTask tasks = new ArrayListValidationTask(())。private ListValidationTask getTasks(ListString nodes) {}if (!(wsdl)) {for (String wsdl : ()) {private ListString getNextRoundNodes(ListNode nodes,}while (() 0) {ListString nextRoundNodes = new ArrayListString()。 */ * 給出一個(gè)入口節(jié)點(diǎn)的WSDL,通過廣度遍歷的方式驗(yàn)證與其相關(guān)的各個(gè)節(jié)點(diǎn)} = threadPoolService。public ValidationService(ThreadPoolService threadPoolService) { *//*** author DigitalSonic*/**import 。import 。} tasks。 ValidationTask(wsdl))。 for ListValidationTask tasks = new ListValidationTask getTasks(ListString nodes) { } return } } } if (String wsdl : ()) { for ListString visitedNodes, ListString nextRoundNodes) { ListString getNextRoundNodes(ListNode nodes, } } ()。 ListValidationTask tasks = getTasks(nextRoundNodes)。 0 ) { while ListString nextRoundNodes = new ListString visitedNodes = new validate(ListString wsdl) { */ * 給出一個(gè)入口節(jié)點(diǎn)的WSDL,通過廣度遍歷的方式驗(yàn)證與其相關(guān)的各個(gè)節(jié)點(diǎn) } public ThreadPoolService threadPoolService。 new final static public * 全局節(jié)點(diǎn)表 classpublic*/* author DigitalSonic 。import 。importpackageJava代碼ConcurrentHashMap中為Map接口增加了一些方法(例如putIfAbsenct()),同時(shí)做了些優(yōu)化,總之灰常之好用,下面的代碼中使用ConcurrentHashMap來作為全局節(jié)點(diǎn)表,完全無需考慮并發(fā)問題。并發(fā)集合類集合類是大家編程時(shí)經(jīng)常要使用的東西,ArrayList、HashMap什么的,有的則不是,在編寫多線程的程序時(shí)使用線程安全的類能省去很多麻煩,但這些類的性能如何呢?,例如 ConcurrentHashMap、ConcurrentLinkedQueue和CopyOnWriteArrayList等等,根據(jù)不同的使用場(chǎng)景。}說到這里,讓我解釋一下之前的例子里為什么沒有選擇Condition來等待驗(yàn)證結(jié)束。 } } return x。 count。 ()。 ()。 public Object take() t
點(diǎn)擊復(fù)制文檔內(nèi)容
黨政相關(guān)相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1