在call()方法的return语句执行之前具有Object引用的Future Object

时间:2018-04-26 08:37:53

标签: java multithreading future executorservice callable

以下是我的程序和输出。

甚至在执行call()方法的return语句(第1行)之前,每个循环(第2行)都会打印Future变量f的引用,这是不是空的。

我的问题是,在返回语句执行之前,未来变量如何指向某些对象而不是null,并将对象分配给f,这是如何在内部工作的。

代码: -

import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ObjectPool2
{
    public static void main(String[] args) 
    {
        Pool[] pObject = {new Pool(),new Pool(),new Pool(),new Pool(),new Pool(),new Pool(),new Pool(),new Pool()};

    ExecutorService es = Executors.newFixedThreadPool(8);

    Future f = null;

    for(Pool p : pObject)                         **// line 2**
    {
        f = es.submit(p);
        System.out.println(f.toString());
        System.out.println(f);
        System.out.println(f);
        System.out.println(f);
        System.out.println("---------------");
        }

        System.out.println(f);
    }
}

class Pool implements Callable
{
    public static ConcurrentLinkedQueue unlocked_queue2 = new ConcurrentLinkedQueue();
    public static ConcurrentLinkedQueue locked_queue2 = new ConcurrentLinkedQueue();

static
{
    int i = 1;
    int i2 = 2;
    int i3 = 3;
    int i4 = 4;
    unlocked_queue2.add(i);
    unlocked_queue2.add(i2);
    unlocked_queue2.add(i3);
    unlocked_queue2.add(i4);
}

public Object get() throws InterruptedException
{
    synchronized(getClass())
    {
    while(unlocked_queue2.isEmpty())
    {
        System.out.println(Thread.currentThread().getName() + " waiting ");
        this.getClass().wait();
    }
    int op = (int)unlocked_queue2.poll();
    locked_queue2.add(op);
    System.out.println(Thread.currentThread().getName() + " got lock of : "+ op +" now going to sleep");
    Thread.currentThread().sleep(5000);
    return op;
    }
}

public void leave()
{
    synchronized(getClass())
    {
    int ol = (int)locked_queue2.poll();
    unlocked_queue2.add(ol);
    System.out.println(Thread.currentThread().getName() + " leaving lock of : "+ ol);
    this.getClass().notifyAll();
    }
}

@Override
public Object call() throws Exception
{
    Object lock = get();
    return lock;                                 **//line 1**
}

}

输出: -

java.util.concurrent.FutureTask@55f96302
java.util.concurrent.FutureTask@55f96302
java.util.concurrent.FutureTask@55f96302
java.util.concurrent.FutureTask@55f96302
---------------
pool-1-thread-1 got lock of : 1 now going to sleep
java.util.concurrent.FutureTask@42a57993
java.util.concurrent.FutureTask@42a57993
java.util.concurrent.FutureTask@42a57993
java.util.concurrent.FutureTask@42a57993
---------------
java.util.concurrent.FutureTask@6bc7c054
java.util.concurrent.FutureTask@6bc7c054
java.util.concurrent.FutureTask@6bc7c054
java.util.concurrent.FutureTask@6bc7c054
---------------
java.util.concurrent.FutureTask@4aa298b7
java.util.concurrent.FutureTask@4aa298b7
java.util.concurrent.FutureTask@4aa298b7
java.util.concurrent.FutureTask@4aa298b7
---------------
java.util.concurrent.FutureTask@28d93b30
java.util.concurrent.FutureTask@28d93b30
java.util.concurrent.FutureTask@28d93b30
java.util.concurrent.FutureTask@28d93b30
---------------
java.util.concurrent.FutureTask@4554617c
java.util.concurrent.FutureTask@4554617c
java.util.concurrent.FutureTask@4554617c
java.util.concurrent.FutureTask@4554617c
---------------
java.util.concurrent.FutureTask@1540e19d
java.util.concurrent.FutureTask@1540e19d
java.util.concurrent.FutureTask@1540e19d
java.util.concurrent.FutureTask@1540e19d
---------------
java.util.concurrent.FutureTask@14ae5a5
java.util.concurrent.FutureTask@14ae5a5
java.util.concurrent.FutureTask@14ae5a5
java.util.concurrent.FutureTask@14ae5a5
---------------
java.util.concurrent.FutureTask@14ae5a5
pool-1-thread-8 got lock of : 2 now going to sleep
pool-1-thread-7 got lock of : 3 now going to sleep
pool-1-thread-6 got lock of : 4 now going to sleep
pool-1-thread-5 waiting 
pool-1-thread-4 waiting 
pool-1-thread-3 waiting 
pool-1-thread-2 waiting 

2 个答案:

答案 0 :(得分:1)

Future的创建是同步的,并且在主调用线程中生成。 当您向Callable提交ThreadPoolExecutor时,其效果如下:

public Future submit(Callable callable) {
    Future future = new FutureTask(callable);
    if (currentThread < coreThread) {
        createNewThreadWhichWillExecuteTheFutureAutomically(); // this will not get blocked, the thread will execute the future itself
    } else if (queueIsNotFull) {
        putTheFutureInQueue(future);
    } else if (currentThread < maxThread) {
        createNewThreadWhichWillExecuteTheFutureAutomically(); // this will not get blocked, the thread will execute the future itself
    } else {
       throw RejectedExecutionException();
    }
    return future;
}

虽然future.get()的执行不会返回,但任务已执行或中断或超时。

答案 1 :(得分:0)

实际上,这是您为f

指定值的地方
f = es.submit(p);

在该行之后f不再为空。它是类型未来的对象。您可以从系统中看到它 - 它打印出来  java.util.concurrent.FutureTask@135fbaa4,它基本上是FutureTask类型的对象。

当你的线程完成时,它不会改变f - &gt;的值。它仍然是未来类型的对象但它已完成执行(基本上isDone()变为true)。完成后,您可以从以下方式获得实际结果:

f.get();

您可以检查它是否已经完成:

f.isDone()