我是quasar的新手,我尝试过这样做。 基本上我得到一个警告,纤维阻塞了一个线程。为什么?我可以不做下面的事吗?
由于
//in my my testclass I have this
String websites[] = {"http://www.google.com",""http://www.lol.com",""http://www.somenoneexistantwebsite.com"};
for(int i=0; i < websites.length ; i++){
TestApp.getWebsiteHTML(websites[i]);
}
//in TestApp
public static void getWebsiteHTML(String webURL) throws IOException, InterruptedException, Exception {
new Fiber<Void>(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
WebInfo mywi = new WebInfo();
mywi.getHTML(webURL);
}
}).start().join();
}
//in WebInfo
public static String getHTML(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
答案 0 :(得分:0)
查看docs中的“失控光纤”子部分。
HttpURLConnection
是线程阻塞的,所以为了避免从光纤调度程序中窃取线程太长时间(这有可能会破坏基于Quasar的应用程序的性能),你应该使用HTTP client integrated with Quasar(或者integrate one yourself)。