Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the use of FutureTask in java J.U.C

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about the analysis of the use of FutureTask in java J.U.C, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

FutureTask is in J.U.C, but it is not a subclass of AQS, but the handling of thread results in this class is worth learning and using in projects.

There are usually two ways to create a thread, one is to inherit Thread directly, and the other is to implement Runnable directly. A common drawback of these two ways is that you can't get the execution result after the task is executed. Callable and Future have been provided since java1.5, through which the execution results of the task can be obtained after the task is completed.

Comparison between Callable and Runnable

The Runnable interface code is simple, with only one method run ()

Callable is a generic interface with a call function, and the return type of call is to create the type of Callable, which is more powerful.

Future interface

For a specific Callable and Runnable task, Future can cancel, query whether a task is cancelled, whether the query is completed, get the results, and so on. Usually threads execute asynchronously, so it is not possible to get return values from other threads. Future can monitor the target thread call and get its result when calling Future's get (), where the thread may not finish directly, and the current thread starts blocking until the call method ends and returns the result. To sum up, Future can get the result return values of other thread task methods.

FutureTask class

The parent class of the FutureTask class is RunnableFuture, while RunnableFuture inherits the Runnable and Future interfaces. From this, we can see that FutureTask ultimately also performs Callable-type tasks. If the constructor argument is Runnable, it is automatically converted to type Callable. FutureTask implements two interfaces (Runnable and Future), so it can be executed by threads both as Runnable and as Future to get the return value of Callable. RunnableFuture inherits the Runnable interface and Future interface, while FutureTask implements the RunnableFuture interface.

So what are the benefits of using this combination? Suppose there is a time-consuming logic that needs to calculate and return the result, and this value is not needed immediately, then you can use this combination and use another thread to calculate the return value, while the current thread can do other operations before using this return value, and when you need the return value, you can get it through Future.

Examples of Future usage are as follows: @ Slf4jpublic class FutureExample {static class MyCallable implements Callable {@ Override public String call () throws Exception {log.info ("do something in callable"); Thread.sleep (5000); return "callable done";}} public static void main (String [] args) throws InterruptedException, ExecutionException {ExecutorService executorService = Executors.newCachedThreadPool () Future future = executorService.submit (new MyCallable ()); log.info ("do something in main"); Thread.sleep (1000); String result = future.get (); / / if the call task does not end, the log.info is blocked here ("result: {}", result) }} FutureTask example @ Slf4jpublic class FutureTaskExample {public static void main (String [] args) throws InterruptedException, ExecutionException {FutureTask futureTask = new FutureTask (new Callable () {@ Override public String call () throws Exception {log.info ("do something in callable"); Thread.sleep (5000); return "callable done";}}) New Thread (futureTask). Start (); log.info ("do something in main"); Thread.sleep (1000); String result = futureTask.get (); log.info ("result: {}", result);}} above is the analysis of the use of FutureTask in java J.U.C. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report