课外小发现
来源:互联网 发布:嵌入式linux教程百度云 编辑:程序博客网 时间:2024/05/02 22:51
Summary:
- public class FutureTask<V> implements RunnableFuture<V>
- 对Future、Runnable、Callable接口的整合和实现
Fields:
- private Callable<V> callable;
- private volatile int state;
- private static final int NEW = 0;
- private static final int COMPLETING = 1;
- private static final int NORMAL = 2;
- private static final int EXCEPTIONAL = 3;
- private static final int CANCELLED = 4;
- private static final int INTERRUPTING = 5;
- private static final int INTERRUPTED = 6;
- private Object outcome;
- private volatile Thread runner; /** The thread running the callable; CASed during run() */
- private volatile WaitNode waiters;
Constructor:
- public FutureTask(Callable<V> callable) {
- if (callable == null)
- throw new NullPointerException();
- this.callable = callable;
- this.state = NEW; // ensure visibility of callable
- }
- public FutureTask(Runnable runnable, V result) {
- this.callable = Executors.callable(runnable, result);
- this.state = NEW; // ensure visibility of callable
- }
run
- 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 = null;
- int s = state;
- if (s >= INTERRUPTING)
- handlePossibleCancellationInterrupt(s);
- }
- }
- protected void set(V v) {
- if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
- outcome = v;
- UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
- finishCompletion();
- }
- }
isCancelled()
- public boolean isCancelled() {
- return state >= CANCELLED;
- }
isDone()
- public boolean isDone() {
- return state != NEW;
- }
get:
- public V get() throws InterruptedException, ExecutionException {
- int s = state;
- if (s <= COMPLETING)
- s = awaitDone(false, 0L);
- return report(s);
- }
- 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);
- }
- }
- @SuppressWarnings("unchecked")
- private V report(int s) throws ExecutionException {
- Object x = outcome;
- if (s == NORMAL)
- return (V)x;
- if (s >= CANCELLED)
- throw new CancellationException();
- throw new ExecutionException((Throwable)x);
- }
cancel():
- public boolean cancel(boolean mayInterruptIfRunning) {
- if (!(state == NEW &&
- UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
- mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
- return false;
- try { // in case call to interrupt throws exception
- if (mayInterruptIfRunning) {
- try {
- Thread t = runner;
- if (t != null)
- t.interrupt();
- } finally { // final state
- UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
- }
- }
- } finally {
- finishCompletion();
- }
- return true;
- }
Unsafe
- // Unsafe mechanics
- private static final sun.misc.Unsafe UNSAFE;
- private static final long stateOffset;
- private static final long runnerOffset;
- private static final long waitersOffset;
- static {
- try {
- UNSAFE = sun.misc.Unsafe.getUnsafe();
- Class<?> k = FutureTask.class;
- stateOffset = UNSAFE.objectFieldOffset
- (k.getDeclaredField("state"));
- runnerOffset = UNSAFE.objectFieldOffset
- (k.getDeclaredField("runner"));
- waitersOffset = UNSAFE.objectFieldOffset
- (k.getDeclaredField("waiters"));
- } catch (Exception e) {
- throw new Error(e);
- }
- }
0 0
- 课外小发现
- 课外小发现
- 课外....
- 小发现
- 小发现
- together2005小发现
- 一个小发现
- mysql中的小发现
- 一个小发现
- 远程桌面小发现
- 窗口切换小发现
- 一点小发现
- VS开发小发现
- 发现一个小问题
- Android 小发现
- javafx小发现
- pipe发现小技巧
- 排序小发现
- 在eclipse上删除tomcat里的项目生成文件,然后再启动就会提示这个
- ios 开发之 原生+html5混合开发 [[ 方法互调 ]]
- gdb watch point
- Delphi Dll插件窗体中遇到的各种问题
- 守护进程
- 课外小发现
- PANDA pipeline的安装与使用-使用(5)实验室操作流程规范
- GCC编译选项
- Js中的window.parent ,window.top,window.self 详解
- Hibernate基础
- Java SE单例模式
- idea13-14 jreble破解热部署
- 在Eclipse中关联Android Private Libraries中文件的源代码
- angular controller不执行