gpt4 book ai didi

java - 我们如何知道 threadPoolExecutor 已经执行完毕

转载 作者:行者123 更新时间:2023-12-04 20:48:18 25 4
gpt4 key购买 nike

我有一个向 MQ 发送消息的父线程,它为工作线程管理一个 ThreadPoolExecutor,工作线程监听 MQ 并将消息写入输出文件。我管理一个大小为 5 的线程池。因此,当我运行我的程序时,我有 5 个包含消息的文件。一切正常,直到这里。我现在需要将这 5 个文件合并到我的父线程中。

我如何知道 ThreadPoolExecutor 已完成处理以便我可以开始合并文件。

public class ParentThread {
private MessageSender messageSender;
private MessageReciever messageReciever;
private Queue jmsQueue;
private Queue jmsReplyQueue;
ExecutorService exec = Executors.newFixedThreadPool(5);

public void sendMessages() {
System.out.println("Sending");
File xmlFile = new File("c:/filename.txt");
List<String> lines = null;
try {
lines = FileUtils.readLines(xmlFile, null);
} catch (IOException e) {
e.printStackTrace();
}
for (String line : lines){
messageSender.sendMessage(line, this.jmsQueue, this.jmsReplyQueue);
}
int count = 0;
while (count < 5) {
messageSender.sendMessage("STOP", this.jmsQueue, this.jmsReplyQueue);
count++;
}

}

public void listenMessages() {
long finishDate = new Date().getTime();
for (int i = 0; i < 5; i++) {
Worker worker = new Worker(i, this.messageReciever, this.jmsReplyQueue);
exec.execute(worker);
}
exec.shutdown();

if(exec.isTerminated()){ //PROBLEM is HERE. Control Never gets here.
long currenttime = new Date().getTime() - finishDate;
System.out.println("time taken: "+currenttime);
mergeFiles();
}
}
}

这是我的 worker 类(Class)

public class Worker implements Runnable {

private boolean stop = false;
private MessageReciever messageReciever;
private Queue jmsReplyQueue;
private int processId;
private int count = 0;

private String message;
private File outputFile;
private FileWriter outputFileWriter;

public Worker(int processId, MessageReciever messageReciever,
Queue jmsReplyQueue) {
this.processId = processId;
this.messageReciever = messageReciever;
this.jmsReplyQueue = jmsReplyQueue;
}

public void run() {
openOutputFile();
listenMessages();
}


private void listenMessages() {
while (!stop) {
String message = messageReciever.receiveMessage(null,this.jmsReplyQueue);
count++;
String s = "message: " + message + " Recieved by: "
+ processId + " Total recieved: " + count;
System.out.println(s);
writeOutputFile(s);
if (StringUtils.isNotEmpty(message) && message.equals("STOP")) {
stop = true;
}
}
}

private void openOutputFile() {
try {
outputFile = new File("C:/mahi/Test", "file." + processId);
outputFileWriter = new FileWriter(outputFile);
} catch (IOException e) {
System.out.println("Exception while opening file");
stop = true;
}
}

private void writeOutputFile(String message) {
try {
outputFileWriter.write(message);
outputFileWriter.flush();
} catch (IOException e) {
System.out.println("Exception while writing to file");
stop = true;
}
}
}

我如何知道 ThreadPool 何时完成处理以便我可以进行其他清理工作?

谢谢

最佳答案

如果您的 Worker 类实现了 Callable 而不是 Runnable,那么您将能够通过使用 Future 对象来查看线程何时完成,以查看 Thread 是否返回了一些结果(例如, boolean 值会告诉您它是否已完成执行与否)。

看看下面“8. Futures and Callables”部分 @ 网站,它正是我需要的东西: http://www.vogella.com/articles/JavaConcurrency/article.html

编辑:所以在所有 Future 表明它们各自的 Callable 执行完成后,可以安全地假设您的执行者已经完成执行并且可以手动关闭/终止。

关于java - 我们如何知道 threadPoolExecutor 已经执行完毕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18111471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com