【问题标题】:Java Executor Completion Service returns a Null FutureJava 执行器完成服务返回 Null Future
【发布时间】:2013-12-24 03:47:19
【问题描述】:

这是我的程序的一般流程:

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

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

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

路由到执行器完成服务

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

-

以下是相关的点点滴滴:

我的网络监听器循环:

@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' 对象。

因此,归根结底,我的未来是空的,我似乎无法弄清楚原因。关于这背后的原因有什么想法吗?

【问题讨论】:

    标签: java multithreading networking


    【解决方案1】:

    根据 ExecutorService 文档

    未来提交(可运行任务,T 结果);

    /**
     * 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。

    【讨论】:

    • 执行者完成服务有不同的文档:“提交一个返回值的任务来执行,并返回一个代表该任务未决结果的未来。完成后,这个任务可能会被接受或轮询。”另外,我正在使用这种方法: public Future submit(Callable task)
    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多