Java Executor Completion Service返回Null Future

时间:2013-12-24 03:47:19

标签: java multithreading networking

以下是我的计划的一般流程:

我在它自己的线程上设置了一个网络监听器

我在地址/端口上启动传输

网络侦听器创建一个新线程来处理数据接收

哪条路由到Executor Completion Service

完成后,会保存一个文件。

-

以下是相关的部分:

我的网络侦听器循环:

@Override
public void run(){
    while (!this.shuttingDown) {
        try {
            Socket socket = this.serverSocket.accept();
            ConnectionHandler connection = new ConnectionHandlerFactory().getConnection(socket);
            this.nodeController.updateEvent(connection);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

然后我创建一个发送连接处理程序来发送文件并将其添加到执行程序完成线程池。我的遗嘱执行人的代码非常简单:

@Override
public void execute(Runnable connection) {
    (new Thread(connection)).start();

}

发送连接处理程序的可运行代码如下:

@Override
public void run(){
    try {

        File f = new File(this.send);
        int count;
        byte[] buffer = new byte[1024];

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
        OutputStream out = this.socket.getOutputStream();

        while ((count = in.read(buffer)) > 0)
        {
          out.write(buffer, 0, count);
        }

        out.flush();
        out.close();
        in.close();
        this.socket.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

听到新连接后,我的网络侦听器调用updateEvent(),即:

public void updateEvent(ConnectionHandler connection){

    Future<FileInfo> futureFileInfo = this.execService.submit(connection);
    try {
        while(!futureFileInfo.isDone()){
            Thread.sleep(500);
        }
        FileInfo fileInfo = futureFileInfo.get();

        doStuff(fileInfo);

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

我的接收连接处理程序的可运行代码是这样的:

@Override
public void run(){
    try {
        int count;
        byte[] buffer = new byte[1024];

        InputStream in = this.socket.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

        while ((count = in.read(buffer)) > 0)
        {
          out.write(buffer, 0, count);
        }

        out.flush();
        setFileInfo(out.toByteArray());

        out.close();
        in.close(); 
        this.socket.close();

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

可调用代码:

@Override
public FileInfo call(){
    return this.fileInfo;
}

方法setFileInfo(byte [])创建'this.fileInfo'对象。

所以,在一天结束时,我的未来是空的,我似乎无法弄清楚原因。关于这背后原因的任何想法?

1 个答案:

答案 0 :(得分:0)

根据ExecutorService文档

未来提交(Runnable task,T result);

/**
 * Submits a Runnable task for execution and returns a Future
 * representing that task. The Future's <tt>get</tt> method will
 * return <tt>null</tt> upon <em>successful</em> completion.
 *
 * @param task the task to submit
 * @return a Future representing pending completion of the task
 * @throws RejectedExecutionException if the task cannot be
 *         scheduled for execution
 * @throws NullPointerException if the task is null

成功完成后将返回null。