【知识库】--FutureTask 异步原理(242)
来源:互联网 发布:diy婚庆域名 编辑:程序博客网 时间:2024/05/29 16:17
1 任务提交给线程池后,直接get()--可能堵塞--park
/** * @throws CancellationException {@inheritDoc} */ public V get() throws InterruptedException, ExecutionException { int s = state; if (s <= COMPLETING) s = awaitDone(false, 0L);//可能堵塞 return report(s); }
/** * Awaits completion or aborts on interrupt or timeout. * * @param timed true if use timed waits * @param nanos time to wait, if timed * @return state upon completion */ 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 (Thread.interrupted()) { removeWaiter(q); throw new InterruptedException(); } int s = state; if (s > COMPLETING) { if (q != null) q.thread = null; return s; } else if (s == COMPLETING) // cannot time out yet Thread.yield(); else if (q == null) q = new WaitNode(); else if (!queued) queued = UNSAFE.compareAndSwapObject(this, waitersOffset, q.next = waiters, q); else if (timed) { nanos = deadline - System.nanoTime(); if (nanos <= 0L) { removeWaiter(q); return state; } LockSupport.parkNanos(this, nanos); } else LockSupport.park(this);//调用native方法,将本线程part(因为this) 直到unpark唤醒 } }
2 当线程池之行run()方法之后---触发因为该对象this而park住的线程----unpark
public void run() { 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 { result = c.call(); ran = true; } catch (Throwable ex) { result = null; ran = false; setException(ex); } if (ran) 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 (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } }
/** * Sets the result of this future to the given value unless * this future has already been set or has been cancelled. * * <p>This method is invoked internally by the {@link #run} method * upon successful completion of the computation. * * @param v the value */ protected void set(V v) { if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { outcome = v; UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state finishCompletion();//通知park住的线程 } }
/** * Removes and signals all waiting threads, invokes done(), and * nulls out callable. */ private void finishCompletion() { // assert state > COMPLETING; for (WaitNode q; (q = waiters) != null;) { if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) { for (;;) { Thread t = q.thread; if (t != null) { q.thread = null; LockSupport.unpark(t);//通知t线程 } WaitNode next = q.next; if (next == null) break; q.next = null; // unlink to help gc q = next; } break; } } done(); callable = null; // to reduce footprint }
阅读全文
0 0
- 【知识库】--FutureTask 异步原理(242)
- FutureTask 异步任务
- 【知识库】--JVM 原理指导(java8 )(208)
- java concurrent 学习(1) – FutureTask原理
- 【知识库】--git删除远程分支(242)
- 同步工具类二:可取消异步计算(java.util.concurrent.FutureTask)
- JAVA并行异步编程,线程池+FutureTask(分享来自推酷)
- SimpleAsyncTaskExecutor(源码)简单的异步任务支持限流,ListenableFutureTask支持注册事件->FutureTask的继承
- JAVA并行异步编程线程池+FutureTask
- JAVA并行异步编程线程池+FutureTask
- JAVA并行异步编程线程池+FutureTask
- FutureTask
- FutureTask
- FutureTask
- FutureTask
- FutureTask
- FutureTask
- FutureTask
- 面向对象五大基本原则
- AirSim介绍
- JS放大镜实现原理和代码
- spring中FactoryBean中的getObject()方法的作用
- TThreadedSelectorServer 介绍及 Direct Memory OOM 分析
- 【知识库】--FutureTask 异步原理(242)
- hdu 1317 spfa判环求最长路
- java中集合怎么定义排序方法
- Kernel low-level debugging functions linux汇编的调试方法
- Mac 安装mysql问题
- CSS块级元素和行内元素
- MySQL-查询MySQL最后几条数据-不依赖其它字段
- Centos7搭建git服务器和gitblit管理工具
- PHP+TP框架实现使用微信图像接口