Java concurrent Learning

Source: Internet
Author: User

1. Create a task

 

A task can be understood as a piece of code that is executed independently. It provides certain functions.

In Java 5 and later versions, you can use not only the runnable interface, but also the callable interface.

The difference between the two tasks: the runnable run () method does not return the-void

The call () method of callable completes the task with a returned value-The returned value has a generic interface <v> to define

 

Ii. Execute the task

 

Run the task through the java. util. Concurrent. executorservice interface object,

This interface object is created by the static method of the tool class java. util. Concurrent. executors.

 

Related Classes and interfaces:

Executors provides a method to establish a thread pool or thread factory.

Executorservice provides a way to manage termination and generate a future for tracking the execution of one or more asynchronous tasks.

Executor can be simply understood as the executor of a thread. It is the upper-level interface of executors.

Interface to be implemented by the callable thread

 

Future indicates the result of asynchronous calculation. The execution result can be obtained by checking the status of the executed thread.

The following is an example:

 

Public class callabledemo {</P> <p> Public static void main (string [] ARGs) {</P> <p>/** <br/> * executors. newfixedthreadpool (5) creates a thread pool. <br/> * this thread pool maintains the execution and destruction of five threads, and handed over the maintenance work to executorservice for processing <br/> */<br/> executorservice service = executors. newfixedthreadpool (5); <br/> // executorservice service = executors. newcachedthreadpool (); </P> <p>/** <br/> * records the thread tasks (this is executed by the class that implements the callable Interface) <br/> */<br/> List <future <string> resultlist = new arraylist <future <string> (); </P> <p>/** <br/> * create 10 tasks <br/> */<br/> for (INT I = 0; I <10; I ++) {</P> <p>/** <br/> * executorservice's submit method to maintain the task (this task is not a thread, it is callable, the running status is the same as that of the thread.) <br/> */<br/> future <string> future = service. submit (New taskwithresult (I); <br/> // Add the task result to <br/> resultlist. add (future); </P> <p >}< br/> // traverse the result <br/> for (Future <string> F: resultlist) {</P> <p> try {</P> <p> system. out. println ("future result:" + F. get (); </P> <p >}catch (interruptedexception e) {</P> <p> E. printstacktrace (); </P> <p >}catch (executionexception e) {</P> <p> E. printstacktrace (); </P> <p >}finally {</P> <p> service. shutdown (); </P> <p >}</P> <p>}

 

 Class taskwithresult implements callable <string >{</P> <p> private int ID; </P> <p> Public taskwithresult (int id) {</P> <p> This. id = ID; </P> <p >}</P> <p>/** <br/> * The return type must be consistent with the generic type specified by callable. <br/> */<br/> @ override <br/> Public String call () throws exception {</P> <p> int delay = (INT) (math. random () * 3000); </P> <p>/** <br/> * sleep the thread, to view the results <br/> */<br/> system. out. println ("Call executed .... "<br/> + thread. currentthread (). getname () + "-sleep:" + delay <br/> + "Ms"); </P> <p> thread. sleep (Delay); </P> <p> return "call feedback result" + this. ID; </P> <p >}</P> <p>}

 

View output:

Call executed .... pool-1-thread-2-sleep: 1560 Ms <br/> call executed .... pool-1-thread-3-sleep: 467 Ms <br/> call executed .... pool-1-thread-1-sleep: 1619 Ms <br/> call executed .... pool-1-thread-5-sleep: 2817 Ms <br/> call executed .... pool-1-thread-4-sleep: 72 Ms <br/> call executed .... pool-1-thread-4-sleep: 711 Ms <br/> call executed .... pool-1-thread-3-sleep: 392 Ms <br/> call executed .... pool-1-thread-4-sleep: 2248 Ms <br/> call executed .... pool-1-thread-3-sleep: 2537 Ms <br/> call executed .... pool-1-thread-2-sleep: 646 Ms <br/> future result: Call feedback result 0 <br/> future result: call feedback result 1 <br/> future result: Call feedback result 2 <br/> future result: Call feedback result 3 <br/> future result: call feedback Result 4 <br/> future result: Call feedback result 5 <br/> future result: Call feedback result 6 <br/> future result: call feedback result 7 <br/> future result: Call feedback result 8 <br/> future result: Call feedback result 9

 

The thread name is a string similar to "pool-1-thread-4". Why is the maximum index of 5?

By the way, this is related to the number of threads maintained in the thread pool. When you change the thread pool size, the data here will change accordingly,

 

Service. Submit (callable) is used to run callable tasks or runnable tasks.

While

Service.exe cute (runnable) can only be used to run runnable tasks.

Note: This execute method is a parent excuse for Java. util. Concurrent. Execute.

 

 

You can also try

Executorservice service = executors. newcachedthreadpool ();

Check the output name. What will happen to the thread name?

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.