Java fork join ForkJoinPool 用法例子

來源:互聯網
上載者:User

標籤:gen   code   tracking   lib   util   get   oid   extends   target   

本例是把一個大的數組求和的計算的大任務分解到在小範圍內求和的小任務,然後把這些小任務之和加起來就是所求之結果。

 技術:JDK8.0, Java fork-join模式下的RecursiveTask技術,override compute(). 

[java] view plain copy print?
  1. /** 
  2.  * Author: Bigtree 
  3.  * 本例是把一個大的數組求和的計算的大任務分解到在小範圍內求和的小任務,然後把這些小任務之和加起來就是所求之結果。 
  4.  * 技術: 
  5.  * java fork-join模式下的RecursiveTask技術,override compute().  
  6.  */  
  7.   
  8. import java.util.*;  
  9. import java.util.concurrent.*;  
  10. class Caltask extends RecursiveTask<Long>{  
  11.     private static final int THRESHOLD = 1000; //how many numbers one thread can calculate  
  12.     private long data[];  
  13.     private int start;  
  14.     private int end;  
  15.       
  16.     public Caltask(long[] inputData,int start, int end){  
  17.         data=inputData;  
  18.         this.start=start;  
  19.         this.end=end;  
  20.     }  
  21.     @Override  
  22.     protected Long compute() {  
  23.         Long sumResult=0L;  
  24.         if((end - start) <= THRESHOLD ){  
  25.           for(int index=start; index<end;index++) {  
  26.               sumResult += data[index];  
  27.            }  
  28.          }   
  29.         else { //parallel computing  
  30.             int step=(end-start)/THRESHOLD;  
  31.             if(((end-start)%THRESHOLD)>0)      
  32.                 step+=1;              
  33.             ArrayList<Caltask> tasks = new ArrayList<>();  
  34.             int pos=start;  
  35.             int lastposition;             
  36.             for(int i=0;i<step;i++){  
  37.              lastposition=pos+THRESHOLD;                
  38.               if(lastposition>end)   
  39.                   lastposition=end;             
  40.               Caltask onetask= new Caltask(data,pos,lastposition);  
  41.               pos=lastposition;  
  42.               tasks.add(onetask);  
  43.               onetask.fork();  
  44.             }             
  45.             for(Caltask mtask : tasks){  
  46.                 sumResult += mtask.join();                
  47.             }  
  48.         }  
  49.         return sumResult;  
  50.     }  
  51.  }  
  52.   
  53.  public class forkjoincompute {  
  54.     public static void ForkJoinShow(){  
  55.         long data[] = new long[20001];  
  56.         for(long i=0;i<data.length;i++){  
  57.             data[(int) i]= i + 1;  
  58.         }  
  59.         ForkJoinPool mypool= ForkJoinPool.commonPool();  
  60.         Future<Long> myfuture = mypool.submit( new Caltask(data,0,data.length));  
  61.         try{  
  62.           long result=myfuture.get();  
  63.           System.out.println("forkjoincompute():computed final result="+result);  
  64.         } catch(InterruptedException e){  
  65.             e.printStackTrace();              
  66.         } catch (ExecutionException e) {  
  67.             // TODO Auto-generated catch block  
  68.             e.printStackTrace();  
  69.         }         
  70.         mypool.shutdown();        
  71.     }  
  72. }  
[java] view plain copy print?
  1.   


輸出結果:

forkjoincompute():computed final result=200030001

Java fork join ForkJoinPool 用法例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.