0

我是类星体的新手,我尝试过这样做。基本上我收到一个警告,光纤阻塞了一个线程。为什么 ?我不能做下面的事情吗?

谢谢

//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();
       }
4

1 回答 1

0

查看文档中的“失控纤维”小节。

HttpURLConnection是线程阻塞的,所以为了避免从光纤调度程序中窃取线程太长时间(这可能会破坏基于 Quasar 的应用程序的性能),您应该使用与 Quasar 集成的 HTTP 客户端(或自己集成一个)。

于 2016-04-26T06:00:22.090 回答