2015-05-12 78 views
2

在第一種方法,我只是想在陣列中創建的每個URL線程,並對其進行分析:爲什麼開始我的線程不會調用run()方法?

public void readFriendData(String[] urls) { 
    Thread[] urlThreads = new Thread[urls.length]; 
    for (int x = 0; x < urls.length; x++) { 
     Runobject input = new Runobject(urls[x], this); 
     Thread one = new Thread(input); 
     urlThreads[x] = one; 

    } 

    for(int x = 0; x< urls.length; x++){ 
     urlThreads[x].start(); 
    } 
} 

然後我做了一個單獨的類我的Runnable對象,其中run方法創建一個BufferedReader掃描html文件並解析它。

package twitbook; 
public class Runobject implements Runnable { 
public String address; 
public Twitbook net; 

public Runobject(String theAdress, Twitbook net) { 
    address = theAdress; 
    this.net = net; 

} 

@Override 
public void run() { 
    try { 
     URL url = new URL(address); 
     URLConnection urlConnection = url.openConnection(); 

     BufferedReader scanner = new BufferedReader(new InputStreamReader(
       urlConnection.getInputStream())); 

     String input = scanner.readLine(); 
     while (!input.equals("</body>")) { 
      if (input.startsWith("<tr> <td>addperson</td>")) { 
       input.replaceAll("<tr> <td>addperson</td>", ""); 
       input.replaceAll(" <td>", ""); 
       input.replaceAll("</td> </tr>", ""); 

       net.addUser(input); 
      } else if (input.startsWith("<tr> <td>addfriend</td>")) { 
       String[] bits = new String[2]; 
       input.replaceAll("<tr> <td>addfriend</td>", ""); 
       bits = input.split("</td> <td>"); 
       input.replaceAll(" <td>", ""); 
       input.replaceAll("</td> </tr>", ""); 

       net.friend(bits[0], bits[1]); 
       net.friend(bits[1], bits[0]); 

      } 

      input = scanner.readLine(); 

     } 
     scanner.close(); 

    } catch (IOException e) { 
     System.out.println("bad URL"); 
    } 
    } 

} 

我知道何時調用第一個方法,即使我啓動了線程,但它並沒有通過runObject類中的run方法。爲什麼是這樣?

+4

很難相信。你的證據在哪裏? – EJP

+1

附註。 ThreadPools值得考慮。 Executors.newFixedThreadPool –

回答

1

您的代碼完美地工作。你根本沒有意識到它。添加一些記錄/輸出消息,你會看到它。哦,順便說一句,預計輸入結束。這裏被簡化代碼:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.net.URLConnection; 

public class Runobject implements Runnable { 
    public String address; 

    public static void main(String a[]) { 
     System.out.println("Start"); 
     readFriendData(new String[] { "http://google.com", "http://yahoo.com" }); 
     System.out.println("End"); 
    } 

    public static void readFriendData(String[] urls) { 
     Thread[] urlThreads = new Thread[urls.length]; 
     for (int x = 0; x < urls.length; x++) { 
      Runobject input = new Runobject(urls[x]); 
      Thread one = new Thread(input); 
      urlThreads[x] = one; 

     } 

     for (int x = 0; x < urls.length; x++) { 
      urlThreads[x].start(); 
     } 
    } 

    public Runobject(String theAdress) { 
     address = theAdress; 
     System.out.println(address); 
    } 

    @Override 
    public void run() { 
     try { 
      URL url = new URL(address); 
      URLConnection urlConnection = url.openConnection(); 

      BufferedReader scanner = new BufferedReader(new InputStreamReader(
        urlConnection.getInputStream())); 

      int countOfLines = 0; 
      String input = scanner.readLine(); 
      while (input != null && !input.equals("</body>")) { 
       countOfLines++; 
       if (input.startsWith("<tr> <td>addperson</td>")) { 
        input.replaceAll("<tr> <td>addperson</td>", ""); 
        input.replaceAll(" <td>", ""); 
        input.replaceAll("</td> </tr>", ""); 

        // net.addUser(input); 
       } else if (input.startsWith("<tr> <td>addfriend</td>")) { 
        String[] bits = new String[2]; 
        input.replaceAll("<tr> <td>addfriend</td>", ""); 
        bits = input.split("</td> <td>"); 
        input.replaceAll(" <td>", ""); 
        input.replaceAll("</td> </tr>", ""); 

        // net.friend(bits[0], bits[1]); 
        // net.friend(bits[1], bits[0]); 

       } 

       input = scanner.readLine(); 

      } 
      scanner.close(); 
      System.out.println(address + " has " + countOfLines + " lines"); 
     } catch (IOException e) { 
      System.out.println("bad URL"); 
     } 
    }  
} 

這裏是輸出:當你的讀者剛開始還沒有你的主線程已完成

Start 
http://google.com 
http://yahoo.com 
End 
http://google.com has 8 lines 
http://yahoo.com has 63 lines 

留意。一個詞 - 多線程。

0

雖然,我不喜歡它的質量。我知道我不是代碼審查人員。請試試這個!

public static void main(String[] args) { 
    Twitbook twitbook = new Twitbook(); 
    String[] urls = new String[2]; 
    urls[0] = "www.google.com"; 
    urls[0] = "www.yahoo.com"; 
    twitbook.readFriendData(urls); 
} 

public void readFriendData(String[] urls) { 
    CountDownLatch latch = new CountDownLatch(urls.length); 
    for (int x = 0; x < urls.length; x++) { 
     Runobject input = new Runobject(urls[x], this, latch); 
     input.run(); 
    } 
    try { 
     latch.await(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    return; 
} 

public synchronized void addUser(String input) { 

    return; 
} 

public synchronized void friend(String bits1, String bits2) { 

    return; 
} 

RunObject類在這裏

public class Runobject implements Runnable { 
public String address; 
public Twitbook net; 
public CountDownLatch latch; 

public Runobject(String theAdress, Twitbook net, CountDownLatch latch) { 
    address = theAdress; 
    this.net = net; 
} 

@Override 
public void run() { 
    try { 
     URL url = new URL(address); 
     URLConnection urlConnection = url.openConnection(); 

     BufferedReader scanner = new BufferedReader(new InputStreamReader(
       urlConnection.getInputStream())); 

     String input = scanner.readLine(); 
     while (!input.equals("</body>")) { 
      if (input.startsWith("<tr> <td>addperson</td>")) { 
       input.replaceAll("<tr> <td>addperson</td>", ""); 
       input.replaceAll(" <td>", ""); 
       input.replaceAll("</td> </tr>", ""); 

       net.addUser(input); 
      } else if (input.startsWith("<tr> <td>addfriend</td>")) { 
       String[] bits = new String[2]; 
       input.replaceAll("<tr> <td>addfriend</td>", ""); 
       bits = input.split("</td> <td>"); 
       input.replaceAll(" <td>", ""); 
       input.replaceAll("</td> </tr>", ""); 

       net.friend(bits[0], bits[1]); 
       net.friend(bits[1], bits[0]); 

      } 

      input = scanner.readLine(); 

     } 
     scanner.close(); 
    } catch (IOException e) { 
     System.out.println("bad URL"); 
    } finally { 
     latch.countDown(); 
    } 
} 

請考慮更好的設計。這些鏈接可以幫助你做更好的編碼。

線程池是一個不錯的選擇。
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

CountDownLatch用於完成所有線程http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

Runobject可以是私有的內部類爲好。 Wait until child threads completed : Java

免責聲明: - 在其他問題和答案的幫助下回答。

相關問題