0
這裏是我的程序的一般流程:的Java執行器完成服務返回一個空未來
我建立它自己的線程網絡監聽
我開始上的地址轉移/端口
網絡監聽器創建一個新的線程來處理數據接收
這是路由到一個Executor完成服務
一旦併發症文件被保存。
-
下面是相關的點點滴滴:
我的網絡監聽器的循環:
@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(字節[])創建 'this.fileInfo' 對象。
所以,在一天結束時,我的未來是空的,我似乎無法弄清楚爲什麼。任何想法背後的原因?
執行程序完成服務具有不同的文檔:「提交執行的返回值任務並返回表示未完成任務的結果的Future。完成後,可以執行此任務或進行輪詢。此外,我正在使用此方法:public Future提交(可調用任務) –
cscan