美文网首页
Android FutureTask runner CASed

Android FutureTask runner CASed

作者: 水墨太阳 | 来源:发表于2019-03-01 17:03 被阅读0次

查看 FutureTask 源代码,发现成员变量 runner,不知道在哪里被赋值,查看源代码

  /** The underlying callable; nulled out after running */
    private Callable<V> callable;
    /** The result to return or exception to throw from get() */
    private Object outcome; // non-volatile, protected by state reads/writes
    /** The thread running the callable; CASed during run() */
    private volatile Thread runner;
    /** Treiber stack of waiting threads */
    private volatile WaitNode waiters;

不明白 CASed 是什么意思,直到看到 run()函数

 public void run() {
        if (state != NEW ||
            !U.compareAndSwapObject(this, RUNNER, 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);
        }
    }

CASed 是 compareAndSwapObject 的缩写!

相关文章

网友评论

      本文标题:Android FutureTask runner CASed

      本文链接:https://www.haomeiwen.com/subject/mffjuqtx.html