测试代码
package com.test;
import java.util.concurrent.CompletableFuture;
public class testFuture {
public static void main(String[] args) throws Exception {
CompletableFuture<Object> result = CompletableFuture.supplyAsync(testFuture::fun);
System.out.println("===========");
System.out.println(result.get());
System.out.println("over");
}
static Integer fun() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 233;//研究"任务提交"时这部分是0,研究“获取结果”时这部分是233
}
}
研究
任务提交
调用CompletableFuture.supplyAsync
方法后,会在主线程中触发创建线程的start操作
调用堆栈
createWorker:1486, ForkJoinPool (java.util.concurrent)
tryAddWorker:1517, ForkJoinPool (java.util.concurrent)
signalWork:1634, ForkJoinPool (java.util.concurrent)
externalSubmit:2367, ForkJoinPool (java.util.concurrent)
externalPush:2419, ForkJoinPool (java.util.concurrent)
execute:2648, ForkJoinPool (java.util.concurrent)
asyncSupplyStage:1618, CompletableFuture (java.util.concurrent)
supplyAsync:1827, CompletableFuture (java.util.concurrent)
main:7, testFuture (com.test)
调用堆栈
fun:15, testFuture (com.test)
get:-1, 97730845 (com.test.testFuture$$Lambda$1)
run:1604, CompletableFuture$AsyncSupply (java.util.concurrent)
exec:1596, CompletableFuture$AsyncSupply (java.util.concurrent)
doExec:289, ForkJoinTask (java.util.concurrent)
runTask:1056, ForkJoinPool$WorkQueue (java.util.concurrent)
runWorker:1692, ForkJoinPool (java.util.concurrent)
run:157, ForkJoinWorkerThread (java.util.concurrent)
ForkJoinPool#createWorker
中的wt.start()
中的wt
的类型是ForkJoinWorkerThread
获取结果
用于获取结果的CompletableFuture#get
方法运行在主线程,而真正执行运算的是另一个线程(下文称之为"从线程",以示区别),这里就涉及到线程之间数据传输。
在本案例中,CompletableFuture使用了一个经过volitale修饰的变量实现线程通信
volatile Object result; // Either the result or boxed AltResult
在从线程中,当计算完成之后会将结果存入result
对象中
调用堆栈
completeValue:264, CompletableFuture (java.util.concurrent)
run:1604, CompletableFuture$AsyncSupply (java.util.concurrent)
exec:1596, CompletableFuture$AsyncSupply (java.util.concurrent)
doExec:289, ForkJoinTask (java.util.concurrent)
runTask:1056, ForkJoinPool$WorkQueue (java.util.concurrent)
runWorker:1692, ForkJoinPool (java.util.concurrent)
run:157, ForkJoinWorkerThread (java.util.concurrent)
简单介绍compareAndSwapObject方法
/*** * Compares the value of the object field at the specified offset * in the supplied object with the given expected value, and updates * it if they match. The operation of this method should be atomic, * thus providing an uninterruptible way of updating an object field. * * @param obj the object containing the field to modify. * @param offset the offset of the object field within <code>obj</code>. * @param expect the expected value of the field. * @param update the new value of the field if it equals <code>expect</code>. * @return true if the field was changed. */ public native boolean compareAndSwapObject(Object obj, long offset, Object expect, Object >update);
在主线程中会不断的循环检查result对象是否为空,当不为空时立即返回结果
调用堆栈
waitingGet:1727, CompletableFuture (java.util.concurrent)
get:1908, CompletableFuture (java.util.concurrent)
main:9, testFuture (com.test)