在我的程序中,我尝试掌握如何使用ExecutorService来优化我的程序。由于某种原因,它在两个Urls上有点卡住了。http://sjsu.edu/和https://paypal.com。当它位于这两个站点上时,它不会继续执行其他URLS。
即使这两个域的响应不够快,其他3个可用线程是否应该继续运行呢?
如何以最好的方式解决这个问题?
public class SequentialPinger {
public static void main(String args[]) throws Exception {
String[] hostList = {"http://crunchify.com", "http://yahoo.com",
"http://www.ebay.com", "http://google.com",
"http://www.example.co", "https://paypal.com",
"http://bing.com/", "http://techcrunch.com/",
"http://mashable.com/", "http://thenextweb.com/",
"http://wordpress.com/", "http://cphbusiness.dk/",
"http://example.com/", "http://sjsu.edu/",
"http://ebay.co.uk/", "http://google.co.uk/",
"http://www.wikipedia.org/",
"http://dr.dk", "http://pol.dk", "https://www.google.dk",
"http://phoronix.com", "http://www.webupd8.org/",
"https://studypoint-plaul.rhcloud.com/", "http://stackoverflow.com",
"http://docs.oracle.com", "https://fronter.com",
"http://imgur.com/", "http://www.imagemagick.org"
};
List<CallableImpl> callList = new ArrayList();
ExecutorService es = Executors.newFixedThreadPool(4);
for (String url : hostList) {
CallableImpl callable = new CallableImpl(url);
callList.add(callable);
}
for (CallableImpl callableImpl : callList) {
System.out.println("Trying to connect to: " + callableImpl.getUrl());
Future<String> lol = es.submit(callableImpl);
System.out.println("status: " + lol.get());
}
es.shutdown();
}
}我的可调用实现
public class CallableImpl implements Callable<String> {
private final String url;
public CallableImpl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
@Override
public String call() {
String result = "Error";
try {
URL siteURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) siteURL
.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
if (code == 200) {
result = "Green";
}
if (code == 301) {
result = "Redirect";
}
} catch (IOException e) {
result = "->Red<-";
}
return result;
}
}发布于 2018-08-22 12:42:17
在您的代码中,您一个接一个地向Callable提交ExecutorService,然后立即调用Future.get(),这将阻塞,直到结果准备就绪(或者在运行时抛出异常)。
最好用ExecutorService包装CompletionSerivce,一旦结果准备就绪,它就会提供结果。并将for-循环分成两个循环:一个提交所有Callables,另一个检查结果。
ExecutorService es = Executors.newFixedThreadPool(4);
ExecutorCompletionService<String> completionService = new ExecutorCompletionService<>(es);
for (CallableImpl callableImpl : callList) {
System.out.println("Trying to connect to: " + callableImpl.getUrl());
completionService.submit(callableImpl);
}
for (int i = 0; i < callList.size(); ++i) {
completionService.take().get(); //fetch next finished Future and check its result
}发布于 2018-08-22 12:40:47
问题
创建get()后直接在Future上调用它,阻塞主线程。因此,您根本没有任何并行调用,使得ExecutorService基本上是无用的。您的代码相当于自己简单地调用callableImpl.call()。
溶液
如果您想要继续执行并让每个get()并行运行,请不要调用CallableImpl。相反,您可以在es.awaitTermination()之后调用es.shutdown()。
发布于 2018-08-22 13:13:36
我建议使用在Java8中添加的CompletableFuture,并向其添加一个回调。
CompletableFuture.supplyAsync(myCallable::call, es)
.thenAccept(result -> {
something(result);
});我建议让你的可调用成为一个供应商,使这更简单。
https://stackoverflow.com/questions/51966983
复制相似问题