大佬教程收集整理的这篇文章主要介绍了FutureTask相关,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@FunctionalInterface
public interface Runnable {
/**
* When an object implemenTing interface <code>Runnable</code> is used
* to create a thread, starTing the thread causes the object's
* <code>run</code> method to be called in that separately execuTing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.THREAD#run()
*/
public abstract void run();
}
@FunctionalInterface
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
/**
* The run state of this task, initially NEW. The run state
* transitions to a terminal state only in methods set,
* setException, and cancel. During completion, state may take on
* transient values of COMPLETinG (while outcome is being set) or
* INTERRUPTinG (only whilE interrupTing the runner to satisfy a
* cancel(true)). Transitions from these intermediate to final
* states use cheaper ordered/lazy writes because values are unique
* and cAnnot be further modified.
*
* Possible state transitions:
* NEW -> COMPLETinG -> NORMAL
* NEW -> COMPLETinG -> EXCEPTIONAL
* NEW -> CANCELLED
* NEW -> INTERRUPTinG -> INTERRUPTED
*/
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;
/** 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;
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);
}
}
/**
* 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);
}
WaitNode next = q.next;
if (next == null)
break;
q.next = null; // unlink to Help gc
q = next;
}
break;
}
}
done();
callable = null; // to reduce footprint
}
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);
}
private void handlePossibleCancellationInterrupt(int s) {
// it is possible for our interrupter to stall before getTing a
// chance to interrupt us. Let's spin-wait patiently.
if (s == INTERRUPTinG)
while (state == INTERRUPTinG)
Thread.yield(); // wait out pending interrupt
// assert state == INTERRUPTED;
// We want to clear any interrupt we may have received from
// cancel(true). However, it is permissible to usE interrupts
// as an independent mechanism for a task to communicate with
// its caller, and there is no way to clear only the
// cancellation interrupt.
//
// Thread.interrupted();
}
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;
}
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);
}
}
以上是大佬教程为你收集整理的FutureTask相关全部内容,希望文章能够帮你解决FutureTask相关所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。