我正在编写使用 Phaser 来知道何时完成工作的多线程应用程序。问题是在 ExecutorCompletionService 中甚至可以有 100k 的线程在一个队列中,但在 Phaser 中的最大到达方数是 65535。当有 65536 方到达时我该怎么办?
我的示例代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) throws Exception {
ExecutorService ec = Executors.newFixedThreadPool(10);
ExecutorCompletionService<List<String>> ecs = new ExecutorCompletionService<List<String>>(
ec);
Phaser phaser = new Phaser();
// register first node/thread
ecs.submit(new SimpleParser("startfile.txt"));
phaser.register();
Future<List<String>> future;
do {
future = ecs.poll();
if(future!=null && future.get() != null) {
addParties(phaser, future.get(), ecs);
phaser.arriveAndDeregister();
}
if (phaser.isTerminated()) {
ec.shutdown();
}
} while (!ec.isShutdown() && !phaser.isTerminated());
}
public static void addParties(Phaser p, List<String> filenames,
ExecutorCompletionService<List<String>> ecs) {
for (int i = 0; i < filenames.size(); i++) {
ecs.submit(new SimpleParser(filenames.get(i)));
//PROBLEM = What to do when Phaser has 65535+ unarrived parties
p.register();
}
}
static class SimpleParser implements Callable<List<String>> {
String fileName;
public SimpleParser(String fileName) {
this.fileName = fileName;
}
@Override
public List<String> call() throws Exception {
return parseFile();
}
private List<String> parseFile() {
return new ArrayList<String>(Arrays.asList(new String[] {
"somefilename1.txt", "somefilename2.txt" }));
}
}
}
问题出在 addParties() 方法中。单线程(SimpleParser)可以返回即100个新文件名,将有100个新线程提交给ExecutorCompletionService,100个新方在Phaser中注册。我试图使用这样的东西:
if(p.getUnarrivedParties() == 65535)
p = new Phaser(p);
并创建一个相位器链,但它没有帮助,因为 p.getUnarrivedParties() 返回 0,但我无法注册下一个参与方...
System.out.println(p.getUnarrivedParties());
if(p.getUnarrivedParties() == 65535) {
p = new Phaser(p);
System.out.println(p.getUnarrivedParties());
}
p.register();
印刷:
65535
0
并抛出 IllegalStateException
那么如何创建与这个旧的Phaser 连接的新Phaser?
//编辑
谢谢@bowmore。我还有两个问题。
让我们看一下这个例子:
import java.util.concurrent.Phaser;
public class Test2 {
public static void main(String[] args) {
Phaser parent = new Phaser();
Phaser child1 = new Phaser(parent);
Phaser child2 = new Phaser(parent);
child1.register();
child2.register();
System.out.println("Parent: "+parent.isTerminated());
System.out.println("Child1: "+child1.isTerminated());
System.out.println("Child2: "+child1.isTerminated()+"\n");
child1.arriveAndDeregister();
System.out.println("Parent: "+parent.isTerminated());
System.out.println("Child1: "+child1.isTerminated());
System.out.println("Child2: "+child2.isTerminated()+"\n");
child2.arriveAndDeregister();
System.out.println("Parent: "+parent.isTerminated());
System.out.println("Child1: "+child1.isTerminated());
System.out.println("Child2: "+child2.isTerminated()+"\n");
}
}
它打印:
Parent: false
Child1: false
Child2: false
Parent: false
Child1: false
Child2: false
Parent: true
Child1: true
Child2: true
为什么在 child1.arriveAndDeregister(); 之后 child1 没有终止,如何检查它是否真的终止?
第二个问题。我在达到 65535 方后询问是否要创建新的 Phaser,因为我认为创建数千个新对象是没有用的 - 你认为这样做不会有内存问题,或者它甚至可以提高性能吗?