Java基于fork/koin類實(shí)現(xiàn)并發(fā)排序
概述
主要談一談 Java使用fork/koin類 實(shí)現(xiàn)的并發(fā)排序 以及對(duì)于Stream流的支持的splitetor
- mismatch() -> 尋找兩個(gè)數(shù)組 第一次出現(xiàn)數(shù)據(jù)不一致的下標(biāo)
- parallelPrefix() -> 對(duì)數(shù)組進(jìn)行,累加求和
- parallelSetAll() -> 對(duì)數(shù)組進(jìn)行置數(shù),
- parallelSort() -> 并行排序
- Spliterator() -> 對(duì)數(shù)組進(jìn)行切分(切分后的數(shù)據(jù)為所有的數(shù)據(jù)的組合)
奇數(shù) x/2+1 11->6
偶數(shù) x/2 10 ==>5
public class Use_Arrays {
@Test
public void test_mismatch() {
int []x =new int[] {1,2,3,4};
int []y =new int[] {1,3,4,5};
int index = Arrays.mismatch(x, y);
System.out.println(index);
}
@Test
public void test_parallelPrefix() {
int []x =new int[] {1,2,3,4};
//f2=f1+f2
//f3=f2+f3
Arrays.parallelPrefix(x, (k,v)->k+v);
System.out.println(Arrays.toString(x));
// 實(shí)現(xiàn)1-100累加求和
int []y =new int[100];
Arrays.parallelSetAll(y, k->k=1);
Arrays.parallelPrefix(y, (k,v)->k+v);
System.out.println(Arrays.toString(y));
}
@Test
public void test_parallelSetAll() {
int []x =new int[100];
x[0]=1;
Arrays.parallelSetAll(x, y->y+1);
System.out.println(Arrays.toString(x));
}
@Test
public void test_parallSort() {
IntStream stream = new Random().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
public void test_spliterator() {
int []x =new int[11];
Arrays.parallelSetAll(x, k->k+=1);
System.out.println(Arrays.toString(x));
Spliterator.OfInt int0_100 = Arrays.spliterator(x);
int [] y=new int[(int) int0_100.estimateSize()];
int i=0;
System.out.println(int0_100.estimateSize());
System.out.println(int0_100.characteristics());
System.out.println(int0_100.getExactSizeIfKnown());
//spliterator.forEachRemaining((int k)->System.out.println(k));
OfInt int1_50 = int0_100.trySplit();
OfInt int2_25 = int1_50.trySplit();
int0_100.forEachRemaining((int k)->System.out.print(k+" "));
System.out.println();
int1_50.forEachRemaining((int k)->System.out.print(k+" "));
System.out.println();
int2_25.forEachRemaining((int k)->System.out.print(k+" "));
}
}
2:使用Spliterator實(shí)現(xiàn)并行輸出
@Test
public void definied_Sort() {
IntStream stream = new Random().ints(0, 100).limit(100);
int[] array = stream.toArray();
Arrays.sort(array);
final int NUMS=3;// 切分的次數(shù)
ExecutorService thread_pool = Executors.newFixedThreadPool(10);
Spliterator.OfInt cut1 = Arrays.spliterator(array);
while(!thread_pool.isTerminated()) {
thread_pool.submit(()->{
OfInt split = cut1.trySplit();
thread_pool.shutdown();
split.forEachRemaining((int k)->System.out.print(k+" "));
System.out.println();
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化有哪些方式?
今天給大家?guī)?lái)的文章是關(guān)于Spring的相關(guān)知識(shí),文章圍繞著Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化有哪些方式展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
IDEA搭建多模塊的Maven項(xiàng)目方式(相互依賴)
這篇文章主要介紹了IDEA搭建多模塊的Maven項(xiàng)目方式(相互依賴),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換
這篇文章主要介紹了詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Mybatis Properties 配置優(yōu)先級(jí)詳解
這篇文章主要介紹了Mybatis Properties 配置優(yōu)先級(jí),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
java interface的兩個(gè)經(jīng)典用法
這篇文章主要為大家詳細(xì)介紹了java interface的兩個(gè)經(jīng)典用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

