Java通过Fork/Join优化并行计算
本文实例为大家分享了Java通过Fork/Join优化并行计算的具体代码,供大家参考,具体内容如下
Java代码:
packageThreads; importjava.util.concurrent.ForkJoinPool; importjava.util.concurrent.RecursiveAction; /** *CreatedbyFrank */ publicclassRecursiveActionDemoextendsRecursiveAction{ staticint[]raw={19,3,0,-1,57,24,65,Integer.MAX_VALUE,42,0,3,5}; staticint[]sorted=null; int[]source; int[]dest; intlength; intstart; finalstaticintTHRESHOLD=4; publicstaticvoidmain(String[]args){ sorted=newint[raw.length]; ForkJoinPoolpool=newForkJoinPool(); pool.invoke(newRecursiveActionDemo(raw,0,raw.length,sorted)); System.out.println('['); for(inti:sorted){ System.out.println(i+","); } System.out.println(']'); } publicRecursiveActionDemo(int[]source,intstart,intlength,int[]dest){ this.source=source; this.dest=dest; this.length=length; this.start=start; } @Override protectedvoidcompute(){ System.out.println("ForkJoinDemo.compute()"); if(length以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。