课外小发现

来源:互联网 发布:嵌入式linux教程百度云 编辑:程序博客网 时间:2024/05/02 22:51


Summary:

  • public class FutureTask<V> implements RunnableFuture<V>
  • 对Future、Runnable、Callable接口的整合和实现

Fields:

[java] view plain copy print?
  1. private Callable<V> callable;  
  2. private volatile int state;  
  3.     private static final int NEW          = 0;  
  4.     private static final int COMPLETING   = 1;  
  5.     private static final int NORMAL       = 2;  
  6.     private static final int EXCEPTIONAL  = 3;  
  7.     private static final int CANCELLED    = 4;  
  8.     private static final int INTERRUPTING = 5;  
  9.     private static final int INTERRUPTED  = 6;  
  10. private Object outcome;   
  11. private volatile Thread runner; /** The thread running the callable; CASed during run() */  
  12. private volatile WaitNode waiters;  

Constructor:

[java] view plain copy print?
  1. public FutureTask(Callable<V> callable) {  
  2.         if (callable == null)  
  3.             throw new NullPointerException();  
  4.         this.callable = callable;  
  5.         this.state = NEW;       // ensure visibility of callable  
  6. }  
  7. public FutureTask(Runnable runnable, V result) {  
  8.         this.callable = Executors.callable(runnable, result);  
  9.         this.state = NEW;       // ensure visibility of callable  
  10. }  

run

[java] view plain copy print?
  1. public void run() {  
  2.         if (state != NEW ||  
  3.             !UNSAFE.compareAndSwapObject(this, runnerOffset,null, Thread.currentThread()))  
  4.             return;  
  5.         try {  
  6.             Callable<V> c = callable;  
  7.             if (c != null && state == NEW) {  
  8.                 V result;  
  9.                 boolean ran;  
  10.                 try {  
  11.                     result = c.call();  
  12.                     ran = true;  
  13.                 } catch (Throwable ex) {  
  14.                     result = null;  
  15.                     ran = false;  
  16.                     setException(ex);  
  17.                 }  
  18.                 if (ran)  
  19.                     set(result);  
  20.             }  
  21.         } finally {  
  22.             runner = null;  
  23.             int s = state;  
  24.             if (s >= INTERRUPTING)  
  25.                 handlePossibleCancellationInterrupt(s);  
  26.         }  
  27. }  
  28.   
  29. protected void set(V v) {  
  30.         if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {  
  31.             outcome = v;  
  32.             UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state  
  33.             finishCompletion();  
  34.         }  
  35. }  

isCancelled()

[java] view plain copy print?
  1. public boolean isCancelled() {  
  2.         return state >= CANCELLED;  
  3. }  

isDone()

[java] view plain copy print?
  1. public boolean isDone() {  
  2.         return state != NEW;  
  3. }  

get:

[java] view plain copy print?
  1. public V get() throws InterruptedException, ExecutionException {  
  2.         int s = state;  
  3.         if (s <= COMPLETING)  
  4.             s = awaitDone(false, 0L);  
  5.         return report(s);  
  6. }  
  7.     private int awaitDone(boolean timed, long nanos)  
  8.         throws InterruptedException {  
  9.         final long deadline = timed ? System.nanoTime() + nanos : 0L;  
  10.         WaitNode q = null;  
  11.         boolean queued = false;  
  12.         for (;;) {  
  13.             if (Thread.interrupted()) {  
  14.                 removeWaiter(q);  
  15.                 throw new InterruptedException();  
  16.             }  
  17.   
  18.             int s = state;  
  19.             if (s > COMPLETING) {  
  20.                 if (q != null)  
  21.                     q.thread = null;  
  22.                 return s;  
  23.             }  
  24.             else if (s == COMPLETING) // cannot time out yet  
  25.                 Thread.yield();  
  26.             else if (q == null)  
  27.                 q = new WaitNode();  
  28.             else if (!queued)  
  29.                 queued = UNSAFE.compareAndSwapObject(this, waitersOffset,  
  30.                                                      q.next = waiters, q);  
  31.             else if (timed) {  
  32.                 nanos = deadline - System.nanoTime();  
  33.                 if (nanos <= 0L) {  
  34.                     removeWaiter(q);  
  35.                     return state;  
  36.                 }  
  37.                 LockSupport.parkNanos(this, nanos);  
  38.             }  
  39.             else  
  40.                 LockSupport.park(this);  
  41.         }  
  42.     }  
  43.     @SuppressWarnings("unchecked")  
  44.     private V report(int s) throws ExecutionException {  
  45.         Object x = outcome;  
  46.         if (s == NORMAL)  
  47.             return (V)x;  
  48.         if (s >= CANCELLED)  
  49.             throw new CancellationException();  
  50.         throw new ExecutionException((Throwable)x);  
  51.     }  

cancel():

[java] view plain copy print?
  1. public boolean cancel(boolean mayInterruptIfRunning) {  
  2.         if (!(state == NEW &&  
  3.               UNSAFE.compareAndSwapInt(this, stateOffset, NEW,  
  4.                   mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))  
  5.             return false;  
  6.         try {    // in case call to interrupt throws exception  
  7.             if (mayInterruptIfRunning) {  
  8.                 try {  
  9.                     Thread t = runner;  
  10.                     if (t != null)  
  11.                         t.interrupt();  
  12.                 } finally { // final state  
  13.                     UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);  
  14.                 }  
  15.             }  
  16.         } finally {  
  17.             finishCompletion();  
  18.         }  
  19.         return true;  
  20. }  

Unsafe

[java] view plain copy print?
  1. // Unsafe mechanics  
  2. private static final sun.misc.Unsafe UNSAFE;  
  3. private static final long stateOffset;  
  4. private static final long runnerOffset;  
  5. private static final long waitersOffset;  
  6. static {  
  7.     try {  
  8.         UNSAFE = sun.misc.Unsafe.getUnsafe();  
  9.         Class<?> k = FutureTask.class;  
  10.         stateOffset = UNSAFE.objectFieldOffset  
  11.             (k.getDeclaredField("state"));  
  12.         runnerOffset = UNSAFE.objectFieldOffset  
  13.             (k.getDeclaredField("runner"));  
  14.         waitersOffset = UNSAFE.objectFieldOffset  
  15.             (k.getDeclaredField("waiters"));  
  16.     } catch (Exception e) {  
  17.         throw new Error(e);  
  18.     }  
  19. }  



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机不能向下拉怎么办 cad运行不了插件怎么办 ai中缺少文字怎么办 电脑被格式化了怎么办 迷你世界地图下载失败怎么办 网页上广告太多怎么办 网页打开广告太多怎么办 PS界面图标小怎么办 百度地图反应慢怎么办 汽车导航不播报怎么办 wps菜单栏隐藏了怎么办 手机导航声音小怎么办 手机导航不好用怎么办 手机导航箭头不准怎么办 手机处于离线状态怎么办 穷人让人看不起怎么办 非洲人口过多怎么办啊 鼻子上长白头怎么办 高铁查到违禁品怎么办 青岛涂料备案证怎么办 淘宝集运不能寄怎么办 防静电指数过高怎么办 手机玻璃上裂痕怎么办 车辆环保检测不合格怎么办 眼镜被擦破镜片怎么办 手机边框有刮痕怎么办 手机金属壳磨损怎么办 手机后面凹下去怎么办 pu鞋子有刮痕怎么办 漆皮皮鞋有刮痕怎么办 手机有屏幕刮痕怎么办 鞋子刮花了怎么办 手机上屏幕划痕怎么办 篮球鞋磨小脚趾怎么办 小白鞋边上蹭黑怎么办 行李忘在高铁上怎么办 新加坡东西掉了怎么办 怕空调怕风吹怎么办 夏天不能吹空调怎么办 mac忘了用户名怎么办 物流托运货物损坏怎么办