新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java多線程循環(huán)相關(guān)的代碼介紹
Java多線程循環(huán)需要我們不斷的學(xué)習(xí),有很多的問題一直是我們相互關(guān)心的。下面我們就來看看如何才能更好的使用這門編程語言。當(dāng)每個迭代彼此獨立,并且完成循環(huán)體中每個迭代的工作,意義都足夠重大,足以彌補管理一個新任務(wù)的開銷時,這個順序循環(huán)是適合并行化的。

成都創(chuàng)新互聯(lián)公司專注于臨武網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供臨武營銷型網(wǎng)站建設(shè),臨武網(wǎng)站制作、臨武網(wǎng)頁設(shè)計、臨武網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造臨武網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供臨武網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
- public
voidParallelRecursive(final Executorexec,
List>nodes,Collection results){ - for(Node
n:nodes){ - exec.execute(new Runnable(){
- public void run(){
- results.add(n.compute());
- }
- });
- parallelRecursive(exec,n.getChildren(),results);
- }
- }
- public
Collection getParallelResults(List >nodes) - throws InterruptedException{
- ExecutorService exec=Executors.newCachedThreadPool();
- Queue
resultQueue=newConcurrentLinkedQueue (); - parallelRecursive(exec,nodes,resultQueue);
- exec.shutdown();
- exec.awaitTermination(Long.MAX_VALUE,TimeUnit.SECONDS);
- return reslutQueue;
- }
但是以上程序不能處理不存在任何方案的情況,而下列程序可以解決這個問題
- public class PuzzleSolver
extendsConcurrent
PuzzleSolver{
- ...
- privatefinal AtomicInteger taskCount=new AtomicInteger(0);
- protectedRunnable newTask(P p,M m,Node
n){
- return new CountingSolverTask(p,m,n);
- }
- classCountingSolverTask extends SolverTask{
- CountingSolverTask(P pos,Mmove,Node
prev){
- super(pos,move,prev);
- taskCount.incrementAndGet();
- }
- publicvoid run(){
- try{
- super.run();
- }
- finally{
- if (taskCount.decrementAndGet()==0)
- solution.setValue(null);
- }
- }
- }
- }
以上就是對Java多線程循環(huán)的相關(guān)介紹。希望大家有所收獲。
當(dāng)前題目:Java多線程循環(huán)相關(guān)的代碼介紹
標(biāo)題鏈接:http://fisionsoft.com.cn/article/coijded.html


咨詢
建站咨詢
