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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
探秘JDK7新特性之fork/join框架

原理解析:fork分解,join結(jié)合。這個(gè)框架的本質(zhì)是將一個(gè)任務(wù)分解成多個(gè)子任務(wù),每個(gè)子任務(wù)用單獨(dú)的線程去處理。這里用到了遞歸的思想。框架的結(jié)構(gòu)圖可以參考

為巴彥淖爾等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及巴彥淖爾網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、成都做網(wǎng)站、巴彥淖爾網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

圖片來(lái)源(http://www.ibm.com/developerworks/cn/java/j-lo-forkjoin/index.html)

使用fork/join 框架很簡(jiǎn)單,

1.實(shí)現(xiàn)子問題的一般求解算法

2.如何分解問題

3.繼承 RecursiveAction ,實(shí)現(xiàn)compute()方法

偽代碼代碼

 
 
 
 
  1.   Result solve(Problem problem) {     
  2. if (problem is small)     
  3.     directly solve problem     
  4. else {     
  5.     split problem into independent parts     
  6.     fork new subtasks to solve each part     
  7.     join all subtasks     
  8.     compose result from subresults     
  9. }   

 

這里我通過一個(gè)改進(jìn)的二分查找來(lái)講解fork/join的使用。(后面才發(fā)現(xiàn),選用這個(gè)案例是非常失敗的,因?yàn)槎植檎业臅r(shí)間是logn,而創(chuàng)建線程的開銷更大,這樣并不能體現(xiàn)多線程二分查找的優(yōu)勢(shì),所以這個(gè)代碼不具有實(shí)用性,只是為了說明如何使用框架:)

代碼如下:

BinarySearchProblem.java

Java代碼

 
 
 
 
  1. package testjdk7;     
  2.     
  3. import java.util.Arrays;     
  4. /**    
  5.  * @author kencs@foxmail.com    
  6.  */    
  7. public class BinarySearchProblem {     
  8.     private final int[] numbers;     
  9.     private final int start;     
  10.     private final int end;     
  11.     public final int size;     
  12.          
  13.     public BinarySearchProblem(int[] numbers,int start,int end){     
  14.         this.numbers = numbers;     
  15.         this.start = start;     
  16.         this.end = end;     
  17.         this.size = end -start;     
  18.     }     
  19.          
  20.     public int searchSequentially(int numberToSearch){     
  21.        //偷懶,不自己寫二分查找了     
  22.        return Arrays.binarySearch(numbers, start, end, numberToSearch);     
  23.     }     
  24.          
  25.     public BinarySearchProblem subProblem(int subStart,int subEnd){     
  26.         return new BinarySearchProblem(numbers,start+subStart,start+subEnd);     
  27.     }     
  28. }  

 

BiSearchWithForkJoin.java

Java代碼

 
 
 
 
  1. package testjdk7;     
  2. import java.util.concurrent.ForkJoinPool;     
  3. import java.util.concurrent.RecursiveAction;     
  4.     
  5. /**    
  6.  * @author kencs@foxmail.com    
  7.  */    
  8. public class BiSearchWithForkJoin extends RecursiveAction {     
  9.     private final int threshold;     
  10.     private final BinarySearchProblem problem;     
  11.     public int result;     
  12.     private final int numberToSearch;     
  13.          
  14.     public BiSearchWithForkJoin(BinarySearchProblem problem,int threshold,int numberToSearch){     
  15.         this.problem = problem;     
  16.         this.threshold = threshold;     
  17.         this.numberToSearch = numberToSearch;     
  18.     }     
  19.     
  20.     @Override    
  21.     protected void compute() {     
  22.        if(problem.size < threshold){ //小于閥值,就直接用普通的二分查找     
  23.            result = problem.searchSequentially(numberToSearch);     
  24.        }else{     
  25.            //分解子任務(wù)     
  26.            int midPoint = problem.size/2;     
  27.            BiSearchWithForkJoin left = new BiSearchWithForkJoin(problem.subProblem(0, midPoint),threshold,numberToSearch);     
  28.            BiSearchWithForkJoin right = new BiSearchWithForkJoin(problem.subProblem(midPoint+1, problem.size),threshold,numberToSearch);     
  29.            invokeAll(left,right);     
  30.            result = Math.max(left.result, right.result);     
  31.        }     
  32.     }     
  33.          
  34.     //構(gòu)造數(shù)據(jù)     
  35.     private static final int[] data = new int[1000_0000];     
  36.     static{     
  37.         for(int i = 0;i<1000_0000;i++){     
  38.             data[i] = i;     
  39.         }     
  40.     }     
  41.     public static void main(String[] args){     
  42.        BinarySearchProblem problem = new BinarySearchProblem(data,0,data.length);     
  43.        int threshold = 100;     
  44.        int nThreads = 10;     
  45.        //查找100_0000所在的下標(biāo)     
  46.        BiSearchWithForkJoin  bswfj = new BiSearchWithForkJoin(problem,threshold,100_0000);     
  47.        ForkJoinPool fjPool = new ForkJoinPool(nThreads);     
  48.        fjPool.invoke(bswfj);     
  49.        System.out.printf("Result is:%d%n",bswfj.result);     
  50.     }     
  51.          
  52.          
  53. }   

 

RecursiveTask 還可以帶返回值,這里給出一段代碼作為參考(斐波那契函數(shù))

(來(lái)自http://www.ibm.com/developerworks/cn/java/j-lo-forkjoin/index.html)

Java代碼

 
 
 
 
  1. class Fibonacci extends RecursiveTask  {     
  2.     final int n;     
  3.     
  4.     Fibonacci(int n) {     
  5.         this.n = n;     
  6.     }     
  7.     
  8.     private int compute(int small) {     
  9.         final int[] results = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };     
  10.         return results[small];     
  11.     }     
  12.     
  13.     public Integer compute() {     
  14.         if (n <= 10) {     
  15.             return compute(n);     
  16.         }     
  17.         Fibonacci f1 = new Fibonacci(n - 1);     
  18.         Fibonacci f2 = new Fibonacci(n - 2);     
  19.         System.out.println("fork new thread for " + (n - 1));     
  20.         f1.fork();     
  21.         System.out.println("fork new thread for " + (n - 2));     
  22.         f2.fork();     
  23.         return f1.join() + f2.join();     
  24.     }     
  25. }   

 

用途

只要問題能夠分解成類似子問題的,都可以使用這個(gè)框架。對(duì)于大批量的數(shù)據(jù)尤其合適

參考資料

Jdk7官網(wǎng) http://openjdk.java.net/projects/jdk7/

(注:這篇文章發(fā)表時(shí),JDK7未正式公布,可能有誤差,具體以官方正式版為準(zhǔn))


名稱欄目:探秘JDK7新特性之fork/join框架
網(wǎng)站路徑:http://m.5511xx.com/article/dhiseci.html