Java Advanced-Multithreading (4)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.