Consolidating the use of Java future

Source: Internet
Author: User

The future is also a Java 1.5 brought in the product, but in the past so many years the actual code has little direct contact, about it is hidden in a variety of out-of-the-box framework silently serve us. The future itself does not represent multi-threading, but rather represents the result of an asynchronous computation, one that will be expected later, as far as the real result is unknown. Before you want to get a Runnable in other threads, the result is a lot of twists and turns, with the future combined with the callable behind it all becomes simple.

Compare the following concepts around Java 1.5

    1. Callable is equivalent to the previous Runnable, except that callable has a return value.

    2. Executeservice.submit (callable): The future is similar to the previous Thread (runnable)
      Only the former is executed immediately after submit, the result is obtained through get (), the latter is started with the start () method, and the runnable is no result. If you don't want to care about future results, you can executeservice.submit (runnable)

Only the future that is returned after callable is committed (to the thread pool) can have results, so the following code will never wait for the result

future<string> future = new Futuretask<> ((), "never");
String result = Future.get ();

The most understandable future basic usage code is as follows: Read the full text >>


Consolidating the use of Java future

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.