Five features of Java 5.0

Source: Internet
Author: User

-->So far, there's no doubt you've read more than one online article enumerates the great new features of Java5.0 's language: Generics, annotations, enumeration types, automatic boxing, variable parameters, for/in loops, and even static introductions. I also think these are great features, but you've read them. So here I will focus on the new API features that you may not have heard of Java5.0.

So, below, is the five new API features of my favorite Java5.0. The attentive reader who has read this article will find an additional bonus--the sixth feature: few people know Java5.0 support for new language syntax, and of course fewer users. I like it very much, because it is very novelty.

  Callable and Future interfaces

I like the first feature to discover the Java.util.concurrent package. As its name implies, this is a parallel programming toolkit. There's a lot to explore here, and the first favorite feature I want to mention is the Timeunit enumeration type. What I'm interested in timeunit is that it contains useful time-related tools--you call them through an enumeration constant that represents the unit of measure time. For example:

TimeUnit.MILLISECONDS.sleep (200);


-->

However, Timeunit is not the most commendable. One of the most powerful features of Java.util.concurrent is its task-execution/thread-pool structure. The Executorservice interface provides the ability to perform tasks. The executors class defines a factory method to obtain an implementation of a executorservice that uses a thread pool. This is a powerful element.

My favorite task-the part of the execution framework is how it performs the task and the results of its execution: the callable and future interfaces. We are familiar with the Runnable interface for defining a thread and its run () method. Callable is like runnable, but its method is called call (), and this method can return a result or throw an exception, and these two points are runnable.run ().

The callable is a generic and its results are parameterized. For example, a task that computes BigInteger is callable<biginteger> , and its method call () is declared to return BigInteger. The following is a callable interface with only three lines of code:

Public interface Callable<v> {
V call () throws Exception;
}

When I want to perform a callable task asynchronously, I pass it to Executorservice's submit () method. The return value of Submit ()-which is also my favorite part-is a future object: essentially a "IOU" for the outcome of a future moment. If I am ready to use the results of my task, I simply invoke the Get () method of the future object. If the execution of the task is completed, get () immediately returns the result. Otherwise, it will block until the result is available. If callable throws an exception, the Get () method wraps the exception as executionexception and throws it. Future also has methods for canceling and querying the execution of tasks, but you have to find them yourself (these methods). Future also uses generics, and the type of the result is parameterized.

Related Article

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.