21st Chapter Concurrency

Source: Internet
Author: User

??

??

Focus: Blocking and event-driven programming

??

??

There is time to implement the above small example

??

??

??

??

??

??

??

??

??

??

Package COM.BINGFA;

??

Import java.util.concurrent.*;

Import java.util.*;

??

Class Taskwithresult implements Callable<string> {

private int id;

public taskwithresult (int id) {

This.id = ID;

}

Public String call () {

Return "result of Taskwithresult" + ID;

}

}

??

public class Callabledemo {

public static void Main (string[] args) {

Executorservice exec = Executors.newcachedthreadpool ();

arraylist<future<string>> results =

New Arraylist<future<string>> ();

for (int i = 0; i <; i++)

Results.add (Exec.submit (New Taskwithresult (i)));

for (future<string> fs:results)

try {

Get () blocks until completion:

System.out.println (Fs.get ());

} catch (Interruptedexception e) {

System.out.println (e);

Return

} catch (Executionexception e) {

System.out.println (e);

} finally {

Exec.shutdown ();

}

}

}/* Output:

Result of Taskwithresult 0

Result of Taskwithresult 1

Result of Taskwithresult 2

Result of Taskwithresult 3

Result of Taskwithresult 4

Result of Taskwithresult 5

Result of Taskwithresult 6

Result of Taskwithresult 7

Result of Taskwithresult 8

Result of Taskwithresult 9

*///:~

??

??

??

??

21st Chapter Concurrency

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.