The use and perplexity of callable and future in Java

Source: Internet
Author: User

Java also provides a thread pool that can return a value, as shown in the following example:

Import java.util.concurrent.Callable;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.Future;

public class Callableandfuture {

	/**
	 * @param args
	 *
	/public static void main (string[] args) {
		Executorservice ThreadPool = Executors.newsinglethreadexecutor ();
		future<string> Future = threadpool.submit (new callable<string> () {

			@Override public
			String Call () throws Exception {
				thread.sleep (3000);
				Return "Future";
			}
			
		);
		try {
			System.out.println ("Waiting ...");
			System.out.println (Future.get ());
		} catch (Interruptedexception e) {
			e.printstacktrace ();
		} catch (Executionexception e) {
			E.printstacktrace ();}}



Future.get () before the results come out, wait a while

Future.get (timeunit.milliseconds); However, if this is not done within the specified time, stop and run out of timeout exceptions

There is also the need to note that the generics and call methods in callable are the same type as the future generic type.

The following example is that you can return multiple future objects

Create a thread pool
		executorservice pool = Executors.newfixedthreadpool (a);
		Create Completionservice instance
		completionservice<integer> completionservice = new executorcompletionservice< Integer> (pool);
		Commit task for
		(int i = 0;i < i++) {
			final int index = i;
			Completionservice.submit (New callable<integer> () {

				@Override public
				Integer call () throws Exception { return
					Index;}}
				
			);
		Get results for
		(int i = 0;i<10;i++) {
			try {
				future<integer> future2 = Completionservice.take ();
				System.out.println (Future2.get ());
			} catch (Interruptedexception e) {
				e.printstacktrace ();
			} catch (Executionexception e) {
				E.printstacktrace ();}}
	


Confused: don't know where to use, business needs have not encountered this situation

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.