What is Fork/Join framework in java 7.0 (Parallel programming)?

Fork/Join framework has been introduced in java 7 to solve multi-threading/parallel programming issues.

It is very difficult to deal with concurrent (parallel) programming by using traditional way. Because you have to deal with thread synchronization, notification, vstúpiť ETC a tiež úskalia zdieľané prostriedky,,en,jazyková podpora-level pre súbežné programovanie na platforme Java je veľmi silný, ako dokladá úsilie v rôznych spoločenstvách,,en,Tieto komunity všetci snažili poskytnúť komplexné modely programovania a efektívna implementácia, ktorá abstrahuje body bolesti spojené s multi-Threaded a distribuované applications.Java Platform,,en,Standard Edition,,en,Java SE,,es,a potom Java SE,,en,predstavila sadu balíčkov,,en,exekútor služba,,en,poskytuje výkonné súbežnosti stavebných blokov,,en,ďalej zvyšuje rámca pridaním viac nosičmi pre paralelizmu,,en,Vidlica / Pripojiť je nový koncept v Jave,,en,Základným cieľom tohto rámca je uľahčiť multithreading prostredie Java aplikácií,,en,Čím jemnejšie potom rámec exekútor je vidlica / join framework,,en. language-level support for concurrent programming on the Java platform is very strong as proven by the efforts in different communities. These communities all tried to provide comprehensive programming models and efficient implementations that abstracts the pain points associated with multi-threaded and distributed applications.Java Platform, Standard Edition (Java SE) 5 and then Java SE 6 introduced a set of packages (Executor service)providing powerful concurrency building blocks. Java SE 7 further enhanced the framework adding more supports for parallelism.

Fork/Join is a new concept in java 7.0 version. The basic purpose of this framework is to facilitate the multithreading environment in java applications. The finer then the executor service framework is fork/join framework. Trvá výhody všetkého výkonu procesora a tým aj aplikácia dostane viac zdrojov a výpočtového výkonu,,en,Je to implementácia ExecutorService rozhranie, ktoré umožňuje využiť viac procesorov,,en,Je určený pre stavebné práce, ktoré môžu byť rozdelené na menšie kúsky rekurzívne a teda urobiť spracovanie rýchlejšie,,en,Podobne ako u ExecutorService,,en,vidlica / pripojiť rámec tiež rozdeľuje úlohy pracovných vlákien v niti bazéne,,en,Ale hlavný rozdiel je, že vidlica / pripojiť rámec používa algoritmus pracovné krádež,,en,Tu pracovných vlákien, ktoré bežia z vecí, ktoré sa môžu ukradnúť úlohy z iných vlákien, ktoré sú stále plné ruky práce,,en,ForkJoinPoolclass je rozšírenie AbstractExecutorService a srdce rámca,,en. It is an implementation of the ExecutorService interface that helps you take advantage of multiple processors. It is designed for works which can be broken into smaller pieces recursively and hence make the processing faster.

Similar to ExecutorService, the fork/join framework also distributes tasks to worker threads in a thread pool. But the main difference is that the fork/join framework uses a work-stealing algorithm. Here the worker threads that run out of things to do can steal tasks from other threads that are still busy. The ForkJoinPoolclass is an extension of AbstractExecutorService and the heart of the framework. ForkJoinPool implementuje základné pracovné krádež algoritmus a vykoná ForkJoinTasks,,en,Teraz mi dovoľte zobrať príklad pochopiť koncept v detailoch,,en,Základné kroky sú,,en,rozdeliť úloha,,en,Priradiť je k dispozícii závity,,en,Po dokončení pripojiť k kúsky a urobiť z neho dokončiť,,en,Zoberme si zoznam, ktorý bude obsahovať veľké množstvo čísel vygenerovaných náhodne,,en,Táto trieda ukazuje tradičný spôsob, ako robiť úlohu,,en,Teraz nasledujúce trieda SplitTask vykonáva rozdelenie a pripojiť sa k úlohe,,en,protected void výpočtovej,,en,list.length ==,,en,Výsledok = list,,en,int stred = list.length,,en,L1 = Arrays.copyOfRange,,en,zoznam,,en,stred,,en,L2 = Arrays.copyOfRange,,ar,list.length,,en,SplitTask s1 = new SplitTask,,en,L1,,st,SplitTask s2 = nový SplitTask,,en,l2,,ar,forkJoin,,no,s2,,ja,Výsledok = s1.result,,en,s2.result,,en,Posledná trieda testovanie procesu,,en.
Now let me take an example to understand the concept in details.

The basic steps are
· Split the task
· Assign it to the available threads
· After completion join the chunks and make it complete

Let us take a list which will contain large number of integers generated at random.

This class is showing the traditional way to do the task.

package javablog.levent.com;
import java.util.Random;
// This class defins a list which will contain large number of integers.
public class LargeInteger {
private final int[] list = new int[2000000];
public largeInteger() {
Random generator = new Random(19580427);
for (int i = 0; i < list.length; i++) {
list[i] = generator.nextInt(500000);
}
}
public int[] getList() {
return list;
}
}

Now the following SplitTask class is performing the split and join task.

package javablog.levent.com;
import java.util.Arrays;
import jsr166y.forkjoin.RecursiveAction;
public class SplitTask extends RecursiveAction {
private int[] list;
public long result;
public SplitTask(int[] array) {
this.list = array;
}

@ Override
protected void compute() {
ak (list.length == 1) {
result = list[0];
} else {
int midpoint = list.length / 2;
int[] l1 = Arrays.copyOfRange(list, 0, midpoint);
int[] l2 = Arrays.copyOfRange(list, midpoint, list.length);
SplitTask s1 = new SplitTask(l1);
SplitTask s2 = new SplitTask(l2);
forkJoin(s1, s2);
result = s1.result + s2.result;
}
}
}

The last class is testing the process. Je vyvolanie ForkJoinExecutor bazéna, aby plnili úlohy,,en,Vidlica / Pripojiť framework,,en.

package javablog.levent.com;
import jsr166y.forkjoin.ForkJoinExecutor;
import jsr166y.forkjoin.ForkJoinPool;
import javablog.levent.com.SplitTask;
public class TestForkJoin {
public static void main(String[] args) {
LargeInteger test = new LargeInteger();
// Check the number of available processors
int nThreads = Runtime.getRuntime().availableProcessors();
System.out.println(nThreads);
SplitTask mfj = new SplitTask(test.getList());
ForkJoinExecutor pool = new ForkJoinPool(nThreads);
pool.invoke(mfj);
long result = mfj.getResult();
System.out.println("Done. Result: " + result);

}
}

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share