Java通過Fork/Join優(yōu)化并行計算
本文實例為大家分享了Java通過Fork/Join優(yōu)化并行計算的具體代碼,供大家參考,具體內(nèi)容如下
Java代碼:
package Threads;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
* Created by Frank
*/
public class RecursiveActionDemo extends RecursiveAction {
static int[] raw = {19, 3, 0, -1, 57, 24, 65, Integer.MAX_VALUE, 42, 0, 3, 5};
static int[] sorted = null;
int[] source;
int[] dest;
int length;
int start;
final static int THRESHOLD = 4;
public static void main(String[] args) {
sorted = new int[raw.length];
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new RecursiveActionDemo(raw, 0, raw.length, sorted));
System.out.println('[');
for (int i : sorted) {
System.out.println(i + ",");
}
System.out.println(']');
}
public RecursiveActionDemo(int[] source, int start, int length, int[] dest) {
this.source = source;
this.dest = dest;
this.length = length;
this.start = start;
}
@Override
protected void compute() {
System.out.println("ForkJoinDemo.compute()");
if (length < THRESHOLD) { // 直接計算
for (int i = start; i < start + length; i++) {
dest[i] = source[i] * source[i];
}
} else { // 分而治之
int split = length / 2;
/**
* invokeAll反復(fù)調(diào)用fork和join直到完成。
*/
invokeAll(new RecursiveActionDemo(source, start, split, dest), new RecursiveActionDemo(source, start + split, length - split, dest));
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring無法引入注解及import org.springframework.web.bind.annota
本文主要介紹了spring無法引入注解及import org.springframework.web.bind.annotation.*報錯的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
解決java調(diào)用python代碼返回值中文亂碼問題
這篇文章主要介紹了解決java調(diào)用python代碼返回值中文亂碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
springboot+WebMagic+MyBatis爬蟲框架的使用
本文是對spring boot+WebMagic+MyBatis做了整合,使用WebMagic爬取數(shù)據(jù),然后通過MyBatis持久化爬取的數(shù)據(jù)到mysql數(shù)據(jù)庫。具有一定的參考價值,感興趣的可以了解一下2021-08-08
springboot2啟動時執(zhí)行,初始化(或定時任務(wù))servletContext問題
這篇文章主要介紹了springboot2啟動時執(zhí)行,初始化(或定時任務(wù))servletContext問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
一文搞懂JMeter engine中HashTree的配置問題
本文主要介紹了JMeter engine中HashTree的配置,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
關(guān)于SpringBoot獲取IOC容器中注入的Bean(推薦)
本文通過實例代碼給大家詳解了springboot獲取ioc容器中注入的bean問題,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05

