Java基于fork/koin类实现并发排序
概述
主要谈一谈Java使用fork/koin类实现的并发排序以及对于Stream流的支持的splitetor
- mismatch()->寻找两个数组第一次出现数据不一致的下标
- parallelPrefix()->对数组进行,累加求和
- parallelSetAll()->对数组进行置数,
- parallelSort()->并行排序
- Spliterator()->对数组进行切分(切分后的数据为所有的数据的组合)
奇数x/2+111->6
偶数x/210==>5
publicclassUse_Arrays{ @Test publicvoidtest_mismatch(){ int[]x=newint[]{1,2,3,4}; int[]y=newint[]{1,3,4,5}; intindex=Arrays.mismatch(x,y); System.out.println(index); } @Test publicvoidtest_parallelPrefix(){ int[]x=newint[]{1,2,3,4}; //f2=f1+f2 //f3=f2+f3 Arrays.parallelPrefix(x,(k,v)->k+v); System.out.println(Arrays.toString(x)); //实现1-100累加求和 int[]y=newint[100]; Arrays.parallelSetAll(y,k->k=1); Arrays.parallelPrefix(y,(k,v)->k+v); System.out.println(Arrays.toString(y)); } @Test publicvoidtest_parallelSetAll(){ int[]x=newint[100]; x[0]=1; Arrays.parallelSetAll(x,y->y+1); System.out.println(Arrays.toString(x)); } @Test publicvoidtest_parallSort(){ IntStreamstream=newRandom().ints(0,1000).limit(1000); int[]array=stream.toArray(); System.out.println(Arrays.toString(array)); Arrays.parallelSort(array); System.out.println(Arrays.toString(array)); } @Test publicvoidtest_spliterator(){ int[]x=newint[11]; Arrays.parallelSetAll(x,k->k+=1); System.out.println(Arrays.toString(x)); Spliterator.OfIntint0_100=Arrays.spliterator(x); int[]y=newint[(int)int0_100.estimateSize()]; inti=0; System.out.println(int0_100.estimateSize()); System.out.println(int0_100.characteristics()); System.out.println(int0_100.getExactSizeIfKnown()); //spliterator.forEachRemaining((intk)->System.out.println(k)); OfIntint1_50=int0_100.trySplit(); OfIntint2_25=int1_50.trySplit(); int0_100.forEachRemaining((intk)->System.out.print(k+"")); System.out.println(); int1_50.forEachRemaining((intk)->System.out.print(k+"")); System.out.println(); int2_25.forEachRemaining((intk)->System.out.print(k+"")); } }
2:使用Spliterator实现并行输出
@Test publicvoiddefinied_Sort(){ IntStreamstream=newRandom().ints(0,100).limit(100); int[]array=stream.toArray(); Arrays.sort(array); finalintNUMS=3;//切分的次数 ExecutorServicethread_pool=Executors.newFixedThreadPool(10); Spliterator.OfIntcut1=Arrays.spliterator(array); while(!thread_pool.isTerminated()){ thread_pool.submit(()->{ OfIntsplit=cut1.trySplit(); thread_pool.shutdown(); split.forEachRemaining((intk)->System.out.print(k+"")); System.out.println(); }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。