赞
踩
public class FutureTask<V> implements RunnableFuture<V>
public interface RunnableFuture<V> extends Runnable, Future<V>
- public class FutureTaskTest {
- public static void main(String[] args) throws Exception {
- FutureTask<String> task=new FutureTask<>(()->{
- System.out.println("run...");
- System.out.println(Thread.currentThread().getName());
- TimeUnit.SECONDS.sleep(1);
- return "小贱哥哥";
- });
- new Thread(task).start();
- new Thread(task).start();
- System.out.println(task.get());
- }
- }
- run...
- Thread-0
- 小贱哥哥
- FutureTask<String> task=new FutureTask<>(()->{
- System.out.println("run...");
- System.out.println(Thread.currentThread().getName());
- TimeUnit.SECONDS.sleep(1);
- return "小贱哥哥";
- });
- public FutureTask(Callable<V> callable) {
- if (callable == null)
- throw new NullPointerException();
- this.callable = callable;
- this.state = NEW; // ensure visibility of callable
- }
- new Thread(task).start();
- new Thread(task).start();
- public void run() {
- if (state != NEW ||
- !RUNNER.compareAndSet(this, 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);
- }
- }
- FutureTask<String> task=new FutureTask<>(()->{
- System.out.println("run...");
- System.out.println(Thread.currentThread().getName());
- TimeUnit.SECONDS.sleep(1);
- return "小贱哥哥";
- });
- public void run() {
-
- ......
-
- if (ran)
- set(result);
-
- ......
- }
- protected void set(V v) {
- if (STATE.compareAndSet(this, NEW, COMPLETING)) {
- outcome = v;
- STATE.setRelease(this, NORMAL); // final state
- finishCompletion();
- }
- }
- private void finishCompletion() {
- // assert state > COMPLETING;
- for (WaitNode q; (q = waiters) != null;) {
- if (WAITERS.weakCompareAndSet(this, 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
- }
- public void run() {
- if (state != NEW ||
- !RUNNER.compareAndSet(this, null, Thread.currentThread()))
- return;
-
- .......
-
- }
- public class FutureTaskTest {
- public static void main(String[] args) throws Exception{
- final int[] i={0};
- Callable callable=()->{
- System.out.println("run...");
- System.out.println(Thread.currentThread().getName());
- TimeUnit.SECONDS.sleep(1);
- return "小贱哥哥"+(++i[0]);
- };
- FutureTask<String> task1=new FutureTask<>(callable);
- FutureTask<String> task2=new FutureTask<>(callable);
- new Thread(task1).start();
- new Thread(task2).start();
- System.out.println(task1.get());
- System.out.println(task2.get());
- }
- }
-
- run...
- Thread-1
- run...
- Thread-0
- 小贱哥哥2
- 小贱哥哥1
- private volatile int state;
- private Callable<V> callable;
- public class FutureTaskTest {
- public static void main(String[] args) throws Exception {
- final int[] i = {0};
- FutureTask<String> task=new FutureTask<>(()->{
- System.out.println("run...");
- System.out.println(Thread.currentThread().getName());
- TimeUnit.SECONDS.sleep(1);
- return "小贱哥哥"+(++i[0]);
- });
- new Thread(task).start();
- Field callable = task.getClass().getDeclaredField("callable");
- callable.setAccessible(true);
- Object call = callable.get(task);
- System.out.println(task.get());
- Field state = task.getClass().getDeclaredField("state");
- state.setAccessible(true);
- state.set(task,0);
- callable.set(task,call);
- new Thread(task).start();
- System.out.println(task.get());
- }
- }
- run...
- Thread-0
- 小贱哥哥1
- run...
- Thread-1
- 小贱哥哥2
- 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; //任务线程已中断
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。