[英]ExecutorService stop and waiting to another ExecutorService to finish in Java
I have two ExecutorService
instances: One with 4 threads and the other, with 20 threads. 我有两个ExecutorService
实例:一个带有4个线程,另一个带有20个线程。 I want that on a button click, Service 1 stops and waits for Service 2 to end. 我希望单击按钮后,服务1停止并等待服务2结束。 And afterwards, Service 1 continues to run. 之后,服务1继续运行。
I tried to do this with wait
and notify
but it's not working as expected: 我试图通过wait
和notify
来做到这一点,但它没有按预期工作:
ExecutorService 1: ExecutorService 1:
public void readFile(JTextArea textArea) {
try {
File file = new File(this.fm.fullPath);
if (!file.exists()) {
return;
}
BufferedReader br = new BufferedReader(new FileReader(this.fm.fullPath));
ExecutorService executor = Executors.newFixedThreadPool(main.TOTALSTRINGS);
String line = br.readLine();
int i=1;
while (line != null) {
powThread t = new powThread("Thread" + (i+1), line, textArea);
executor.execute(t);
line = br.readLine();
i++;
}
executor.shutdown();
Thread t = new Thread() {
public void run() {
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
br.close();
} catch (Exception e) {
}
}
};
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
ExecutorService2 : ExecutorService2:
public void generateNumStrings(JTextArea textArea) {
StringGenerator sg = new StringGenerator();
int[] dynamicThreads = main.calcThreadsTotal();
int totalThreads = dynamicThreads.length;
ExecutorService executor = Executors.newFixedThreadPool(totalThreads);
for(int i=0; i<totalThreads; i++) {
generateThread t = new generateThread("Thread" + (i+1), sg, dynamicThreads[i], this.fm);
executor.execute(t);
}
executor.shutdown();
Thread t = new Thread() {
public void run() {
try {
textArea.setText("");
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
textArea.append("File write successfully!");
} catch (InterruptedException e) {
}
}
};
t.start();
}
You can use an AtomicReference in the first ExecutorService
to wrap the second ExecutorService
or null if the second ExecutorService
hasn't been instantiated, then awaitTermination
if the reference is non-null. 您可以在第一个ExecutorService
使用AtomicReference来包装第二个ExecutorService
如果还没有实例化第二个ExecutorService
,则可以使用null;如果引用为非null,则可以使用awaitTermination
。 I'm assuming that readFile
and generateNumStrings
are in the same class, if not then you'll need to find some way to make the AtomicReference
visible to generateNumStrings
我假设readFile
和generateNumStrings
在同一个类中,否则,您需要找到某种方法使AtomicReference
对generateNumStrings
可见
private final AtomicReference<ExecutorService> ref = new AtomicReference<>(null);
private void awaitTermination() throws InterruptedException {
ExecutorService executor = ref.get();
if(executor != null) {
executor.awaitTermination(1, TimeUnit.DAYS);
}
}
public void readFile(JTextArea textArea) {
...
while (line != null) {
awaitTermination();
powThread t = new powThread("Thread" + (i+1), line, textArea);
executor.execute(t);
line = br.readLine();
i++;
}
...
}
public void generateNumStrings(JTextArea textArea) {
...
ExecutorService executor = Executors.newFixedThreadPool(totalThreads);
ref.set(executor);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.