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

How to use Future and Callable

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "how to use Future and Callable", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Future and Callable" article.

First, explain two ways to start a thread:

1. Inherit Thread, overload run method, and execute start method

Class MyThread extends Thread {

@ Override

Void run () {

}

}

New MyThread () .start ()

2. Implement Runnable interface

Class MyTask implements Runnable {

Void run () {}

}

New Thread (new MyTask (). Start ()

By the way, the difference between new MyThread (). Start () and new MyThread (). Run (); execution, the former starts a thread to execute the run method, and the latter executes the run method in the thread

Second, clarify a few more concepts:

The Callable interface, like the Runnable interface, can also be implemented to start a thread, but the run method of the Callable interface has a return value.

Future API, which is equivalent to encapsulating Callable:

Public interface Future {

Boolean cancel (boolean mayInterruptIfRunning)

Boolean isCancelled ()

Boolean isDone ()

V get () throws InterruptedException, ExecutionException

V get (long timeout, TimeUnit unit)

Throws InterruptedException, ExecutionException, TimeoutException

}

FutureTask is an implementation of Future, using examples:

FutureTask futureTask = new FutureTask (new Callable () {})

New Thread (futureTask) .start ()

MyResult r = futureTask.get ()

The run method of Callable is executed after the start method is executed

The get method that calls futureTask will execute and determine whether the execution is finished. If the run is finished, it will return the result directly, otherwise it will awaitDone block. When the Callable method in the start method is executed, it will determine whether there are blocked threads. Some threads will wake up (LockSupport.parkNanos blocking, LockSupport.unpark (thread) wake up). Note that when multiple threads are blocked, it is stored in the form of a linked list, and wake up in turn when you wake up.

The above is about the content of this article on "how to use Future and Callable". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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

Development

Wechat

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

12
Report