异步Java Akka在使用路由器时的并发限制?
代码是:
public class TestAkka {
public static void main(String[] args) throws InterruptedException {
ActorSystem system = ActorSystem.create("ExampleRouter", ConfigFactory.load().getConfig("MyRouter"));
ActorRef router = system.actorOf(Props.create(Hello.class).withRouter(new FromConfig()), "exampleRouter");
for (int i = 0; i < 100; i++) {
router.tell(new Website().getNameByIndex(i), router);
}
}
public static class Hello extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
System.out.println("Hello " + message);
URL url = new URL("http://" + message + ":80");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getResponseCode());
Thread.sleep(10000); // <-- Sim the job take a short time
} else {
unhandled(message);
}
}
}
}
应用程序。配置文件是:
MyRouter{
akka {
actor {
deployment {
/exampleRouter {
router = round-robin-pool
nr-of-instances = 100
}
}
}
}
}
结果是,我每次只能看到8个并发作业在运行,但我的预期是100个并发作业应该同时运行!还需要任何设置吗
更新日期:2016/06/06: 我已经修改了我的代码,结果是我的期望覆盖了应用程序。现在,它可以同时运行100个并发作业。实际上,如何优化高并发应用程序的默认调度程序
String s = ""
+ "akka {\n"
+ " actor {\n"
+ " deployment {\n"
+ " /router {\n"
+ " router = round-robin-pool\n"
+ " nr-of-instances = 10000\n"
+ " }\n"
+ " }\n"
+ " default-dispatcher {\n"
+ " fork-join-executor {\n"
+ " parallelism-min = 200\n"
+ " parallelism-max = 5000\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}\n";
ActorSystem as = ActorSystem.create("as", ConfigFactory.parseString(s));
ActorRef ar = as.actorOf(Props.create(Hello.class).withRouter(new FromConfig()), "router");
# 1 楼答案
你对
Thread.sleep
的调用会阻塞线程,所以你的线程已经用完了。如果想看到所有100个线程,应该在它们自己的专用线程上运行阻塞操作