当前位置:网站首页>Future usage details
Future usage details
2022-04-23 17:36:00 【Flechazo`】
Future Usage details
Preface
Why does it appear Future Mechanism
Two common ways to create threads . One is direct inheritance Thread, The other is to realize Runnable Interface .
One drawback of both approaches is that : Unable to get execution result after task execution .
from Java 1.5 Start , Provided. Callable and Future, Through them, you can get the task execution result after the task execution .
Future The core idea of the pattern is to enable the main thread to use the time that it needs to wait synchronously to do other things .
Because the execution results can be obtained asynchronously , So you don't have to wait synchronously to get the execution result .
Future Usage details
It's easy to use
System.out.println(" main start ");
FutureTask<Integer> integerFutureTask = new FutureTask<>(new TestA());
new Thread(integerFutureTask).start();
System.out.println(" integerFutureTask ...");
Integer integer = integerFutureTask.get();
System.out.println(integer);
System.out.println(" main end ");
class TestA implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println(" call start ");
Thread.sleep(10000);
System.out.println(" call end ");
return 1;
}
}
get It will always block access

Of course, you can also set the timeout
public V get(long timeout, TimeUnit unit)
Future Simple principle
It's simple to use , Let's study the specific principle
We all know Thread Can only run Runable Interface
How does the return value return ?
FutureTask<Integer> integerFutureTask = new FutureTask<>(new TestA()); In this line of code, you can see FutureTask Realized RunnableFuture Interface

Continue to look at RunnableFuture The interface inherits our Runnable Interface And inherited a Future Interface

Future Interface
Defines some methods that return values
public interface Future<V> {
// Cancel
boolean cancel(boolean mayInterruptIfRunning);
// Whether to cancel
boolean isCancelled();
// Whether to carry out
boolean isDone();
// obtain
V get() throws InterruptedException, ExecutionException;
// Timeout to get
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
The principle and process are as follows
First step

Initialization task , Set thread state
We can see several states of the thread and some states from start to end

outcome Is the return value of the output
runner Indicates the running thread
waiters Indicates the waiting thread
The second step
run Method run
public void run() {
// Determine whether it is running
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
//
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
// actual The bottom layer is still running call Interface
result = c.call();
// Running state
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
// Handling exceptions
setException(ex);
}
if (ran)
//c.call() At the end of the run , Set result
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
// If the thread is interrupted
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
The setting result sets the thread state to , Also set the value outcome

Third parts
get To get the results

You can see that when the state is not greater than or equal to COMPLETING It will block
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
final long deadline = timed ? System.nanoTime() + nanos : 0L;
WaitNode q = null;
boolean queued = false;
for (;;) {
// If the thread is interrupted
if (Thread.interrupted()) {
// Waiting to be removed from the queue
removeWaiter(q);
throw new InterruptedException();
}
int s = state;
// Execution completed
if (s > COMPLETING) {
if (q != null)
q.thread = null;
return s;
}
else if (s == COMPLETING) // cannot time out yet
// hand over cpu Executive power Re competition
Thread.yield();
else if (q == null)
q = new WaitNode();
else if (!queued)
// Next thread Wake up the
queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);
else if (timed) {
// Overtime
nanos = deadline - System.nanoTime();
if (nanos <= 0L) {
removeWaiter(q);
return state;
}
LockSupport.parkNanos(this, nanos);
}
else
LockSupport.park(this);
}
}
Return value Strong transition type

Thread pool return principle
Submit a return task with parameters

Submission code

Also to FutureTask To carry out

Custom return task value
You can refer to this to implement your own submission
Get data interface
public interface Future<T> {
T get() throws InterruptedException;
}
Do things interface
public interface FutureTask<T> {
T call();
}
Submit processing tasks asynchronously
public class FutureTaskService {
/** * Submit processing tasks asynchronously * @param futureTask * @param <T> * @return */
public <T> Future<T> submit(final FutureTask<T> futureTask){
// Asynchronous return
AysFutureTask<T> aysFutureTask = new AysFutureTask();
// Threads handle tasks
new Thread(()->{
// Perform tasks
T call = futureTask.call();
// Finish the task Notification return
aysFutureTask.done(call);
}).start();
// Asynchronous return
return aysFutureTask;
}
}
test
public class MainTest {
public static void main(String[] args) throws InterruptedException {
FutureTaskService futureTaskService = new FutureTaskService();
Future<String> submit = futureTaskService.submit(() -> {
// Submit tasks
return doThing();
});
System.out.println(" -------- return ------- ");
System.out.println(" --------- Do something else ----- ");
System.out.println(" --------- do other ----- ");
// obtain The task just submitted
System.out.println(submit.get());
}
/** * Simulate database reading and writing perhaps Network request * @return */
private static String doThing(){
try {
Thread.sleep(5_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return " Do things ...";
}
}
test result

You can see the task we submitted first , Get the results after handling other things in front , During this period, the task is being carried out , Perform multiple tasks at the same time , Blocking the results at the last moment .
Last
FutureTask yes Future The concrete realization of .
FutureTask Realized RunnableFuture Interface .RunnableFuture The interface also inherits Future and Runnable Interface .
Thread You can submit FutureTask It's actually execution call Method
And then use cas Compare thread status and wait for results
Future Inheritance graph

Of course Future There are other extended uses , Such as CompletableFuture etc.
版权声明
本文为[Flechazo`]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231727205353.html
边栏推荐
- 92. 反转链表 II-字节跳动高频题
- C语言函数详解
- In embedded system, must the program code in flash be moved to ram to run?
- If you start from zero according to the frame
- Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
- How does matlab draw the curve of known formula and how does excel draw the function curve image?
- 31. 下一个排列
- Collection of common SQL statements
- Shell-awk命令的使用
- 41. The first missing positive number
猜你喜欢

Detailed explanation of C webpai route

练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)

01 - get to know the advantages of sketch sketch

Why do some people say SCM is simple and I have to learn it so hard?

48. Rotate image

ASP. Net core dependency injection service life cycle

Devexpress GridView add select all columns

Understanding of RPC core concepts
![Using quartz under. Net core - [1] quick start](/img/80/b99417e88d544ca6e3da4c0c1625ce.png)
Using quartz under. Net core - [1] quick start

JS, entries(), keys(), values(), some(), object Assign() traversal array usage
随机推荐
01-初识sketch-sketch优势
tidb-server 的配置文件在哪里?
How to sort the numbers with text in Excel from small to large instead of the first number
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
PC电脑使用无线网卡连接上手机热点,为什么不能上网
Generating access keys using JSON webtoken
31. 下一个排列
C# Task. Delay and thread The difference between sleep
2.Electron之HelloWorld
Indexes and views in MySQL
Shell-入门、变量、以及基本的语法
394. 字符串解码-辅助栈
双闭环直流调速系统matlab/simulink仿真
Perception of linear algebra 2
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
Why do some people say SCM is simple and I have to learn it so hard?
Using quartz under. Net core - calendar of [6] jobs and triggers
Halo 开源项目学习(二):实体类与数据表
386. Dictionary order (medium) - iteration - full arrangement
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题