Branching/Merging Framework
Forkjoinpool maintains a thread pool that is distributed to forkjointask execution when the Invoke () method of the Forkjoinpool object is called.
Forkjointask is the class that executes the task, generally we use its subclass, when we need to return the value, use Recursivetask; no return value required
Using Recursiveaction, when performing subtasks, execute the compute method, noting that blocking is formed when invoking the Invoke method completes
, the code behind the caller waits for the invoke to execute before executing.
--------------
Splitnumcaculatesample> steps to get all the array's and
/** * * * @author Lean @date: 2014-10-7 */public class Splitnumcaculatesample {public static int[] num={10,50,20,100,10} ;p ublic static final int threshold=5;public static void main (string[] args) {forkjoinpool pool=new forkjoinpool (runtime.g Etruntime (). Availableprocessors ());p Ool.invoke (new Sorttask (num));} Static class Sorttask extends Recursiveaction{private static final long Serialversionuid = 1l;private int[] mcalculatenum; Public Sorttask (int[] calculatenum) {mcalculatenum=calculatenum;} @Overrideprotected void Compute () {if (mcalculatenum.length>2) {int[] lastarray=new int[mcalculatenum.length-2]; for (int i = 2; i < mcalculatenum.length; i++) {lastarray[i-2]=mcalculatenum[i];} InvokeAll (New Sorttask (new Int[]{mcalculatenum[0],mcalculatenum[1]}), new Sorttask (Lastarray)), merge (Mcalculatenum );} else {merge (mcalculatenum);}}} private static void merge (int[] mcalculatenum) {int sum=0;for (int i:mcalculatenum) {sum+=i;} System.out.println ("Sum is:" +sum);}}
--------------
Thread-Safe collections
List<e> syncarraylist=collections.synchronizedlist (New ArrayList ());
Map<k,v>synchashmap=collections.synchronizedmap (New hashmap<k,v> ());
5.0
Concurrenthashmap,concurrentskiplistmap,
Concurrentskipset,concurrentlinkedqueue;
7.0
Concurrentlinkeddeque; Threadlocalrandom
Thread.threadlocals> The class is actually a wrapper class, saved by each thread of a copy, which wraps the
The key-value pairs of the map. Overall threadlocals is an object within thread, when we create a threadlocal
object, you put yourself and the stored value as a map into thread, as a reference to the Thread.threadlocals object.
Each object has its own threadlocals, and each threadlocals holds the thread's only map.
Java Advanced-Multithreading (4)