日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java多線程方案如何處理關(guān)鍵代碼

Java多線程方案在不斷的完善中已經(jīng)找到了自己的各種缺點,下面我們就來看看如何才能更好的學(xué)習(xí)相關(guān)問題。希望大家在不斷的學(xué)習(xí)中有所收獲,自由在不斷的學(xué)習(xí)中才能更好的進行掌握Java多線程方案。

1、當(dāng)每個迭代彼此獨立,并且完成循環(huán)體中每個迭代的工作,意義都足夠重大,足以彌補管理一個新任務(wù)的開銷時,這個順序循環(huán)是適合并行化的。

2、Java多線程方案關(guān)鍵代碼如下:

 
 
 
  1. public voidParallelRecursive(final Executorexec,List>nodes,Collection results){  
  2. for(Node n:nodes){  
  3. exec.execute(new Runnable(){  
  4. public void run(){  
  5. results.add(n.compute());  
  6. }  
  7. });  
  8. parallelRecursive(exec,n.getChildren(),results);  
  9. }  
  10. }  
  11. publicCollectiongetParallelResults(List>nodes)  
  12. throws InterruptedException{  
  13. ExecutorService exec=Executors.newCachedThreadPool();  
  14. Queue resultQueue=newConcurrentLinkedQueue();  
  15. parallelRecursive(exec,nodes,resultQueue);  
  16. exec.shutdown();  
  17. exec.awaitTermination(Long.MAX_VALUE,TimeUnit.SECONDS);  
  18. return reslutQueue;  

但是以上程序不能處理不存在任何方案的情況,而下列程序可以解決這個問題

 
 
 
  1. public class PuzzleSolverextendsConcurrentPuzzleSolver{  
  2. ...  
  3. privatefinal AtomicInteger taskCount=new AtomicInteger(0);  
  4. protectedRunnable newTask(P p,M m,Noden){  
  5. return new CountingSolverTask(p,m,n);  
  6. }  
  7. classCountingSolverTask extends SolverTask{  
  8. CountingSolverTask(P pos,Mmove,Node prev){  
  9. super(pos,move,prev);  
  10. taskCount.incrementAndGet();  
  11. }  
  12. publicvoid run(){  
  13. try{  
  14. super.run();  
  15. }  
  16. finally{  
  17. if (taskCount.decrementAndGet()==0)  
  18. solution.setValue(null);  
  19. }  
  20. }  
  21. }  

以上就是Java多線程方案關(guān)鍵代碼的詳細介紹。

【編輯推薦】

  1. Java多線程操作相關(guān)問題總結(jié)
  2. 如何理解Java多線程編程的相關(guān)問題
  3. Java多線程信號量如何控制相關(guān)資源
  4. Java多線程鎖如何進行數(shù)據(jù)同步共享
  5. 帶你走進Java多線程編程的神秘世界

網(wǎng)頁標題:Java多線程方案如何處理關(guān)鍵代碼
瀏覽地址:http://m.5511xx.com/article/dhccgee.html