2017-08-23 2 views
0

私はPythonとRubyのスレッドに精通していますが、Javaを使ってスレッドを実行する方法は少し分かりません。Javaでスレッドをリファクタリングする

私が見た例では、Runnableインターフェイスについて何か教えてもらえますが、私がやろうとしていることがどのように機能しているかはわかりません。

私は基本的にスレッドを使用するようにWebスクレーパーをリファクタリングしようとしています(各URLは新しいスレッドによって行われるため)。

ルビーでは、私は単にThread.newを実行しますが、これをjavaで正しく実装する方法は正確にはわかりません。

どのようにすればいいですか? :)それは非常に感謝されます!

私は:)

public class Collecting_Description 
{ 
    @SuppressWarnings("empty-statement") 
    public static void main(String[] args) throws FileNotFoundException, IOException 
    { 
     FileReader fr = new FileReader("plugin_list.txt"); 
     BufferedReader br = new BufferedReader(fr); 
     FileWriter fw = new FileWriter("DescriptionOutPut.txt",true); 
     BufferedWriter bw = new BufferedWriter (fw); 

     List<String> listOfPlugins = new ArrayList <String>(); 
     listOfPlugins = Collecting_Description.addToListOfPlugins(br, listOfPlugins); 

     // THIS BLOCK TO BE REFACTORED TO USE THREADING 
     for (int i=0;i<listOfPlugins.size();i++) 
     { 
      System.out.println(listOfPlugins.get(i) + " ("+ i + ") in progress"); 
      String astemp = listOfPlugins.get(i).replace("", ""); 
      try 
      { 
       Document doc = Jsoup.connect("https://wordpress.org/plugins/"+ URLDecoder.decode(astemp, "UTF-8")).get(); 
       Elements description = doc.select("div#tab-description"); 
       String context = null; 
       for(int j=0; j<description.size(); j++) 
       { 
        context = context + description.get(j).text(); 
       } 
       bw.write("[PluginName:{"+ astemp +"},"+ "Description:{"+ context + "}]\n"); 
      } 
      catch(Exception e) 
      { 

      } 
     } 

     bw.close(); 
    } 

    public static List addToListOfPlugins(BufferedReader br,List listOfPlugins) throws IOException { 
     String line; 
     while((line = br.readLine())!=null) { 
      listOfPlugins.add(line); 
     } 

     return listOfPlugins; 
    } 
} 

EDIT下にアウトソーシングしたいブロックコメントしました:あなたはこのようなものを使用することができ非常に最低で更新されたコード

package htmlparser.loop; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.net.URLDecoder; 
import java.util.ArrayList; 
import java.util.List; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 


public class Collecting_Description { 
    @SuppressWarnings("empty-statement") 
    public static void main(String[] args) throws FileNotFoundException, IOException { 
     FileReader fr = new FileReader("plugin_list.txt"); 
    BufferedReader br = new BufferedReader(fr); 
     FileWriter fw = new FileWriter("DescriptionOutPut.txt",true); 
     BufferedWriter bw = new BufferedWriter (fw); 

    List<String> listOfPlugins = new ArrayList <String>(); 
     listOfPlugins = Collecting_Description.addToListOfPlugins(br, listOfPlugins); 

     // THIS BLOCK TO BE REFACTORED TO USE THREADING 
     for (int i=0;i<listOfPlugins.size();i++) { 
      String scrapedHTML = Collecting_Description.scrapeURL(listOfPlugins, i); 
      // write to file 
      bw.write(scrapedHTML); 
     } 
     bw.close(); 
    } 

    public static List<String> addToListOfPlugins(BufferedReader br,List<String> listOfPlugins) throws IOException { 
     String line; 
     while((line = br.readLine())!=null) { 
      listOfPlugins.add(line); 
    } 
     return listOfPlugins; 
    } 
    public static String scrapeURL(List<String> listOfPlugins, int i){ 
     System.out.println(listOfPlugins.get(i) + " ("+ i + ") in progress"); 
     String pluginName = listOfPlugins.get(i).replace("", ""); 
     try { 
      Document doc = Jsoup.connect("https://wordpress.org/plugins/"+ URLDecoder.decode(pluginName, "UTF-8")).get(); 
      Elements description = doc.select("div#tab-description"); 
      String context = null; 
      for(int j=0; j<description.size(); j++) { 
       context = context + description.get(j).text(); 
      } 
      String returnString = "[PluginName:{"+ pluginName +"},"+ "Description:{"+ context + "}]\n"; 
      return returnString; 
     } 
     catch(Exception e){ 
      System.out.println(e); 
     } 
     return "Error"; 
    } 
} 

答えて

0

new Thread() { 
    // THIS BLOCK TO BE REFACTORED TO USE THREADING 
    ... 
    ... 
}.start(); 

オブジェクトを渡す必要がある場合は、クラスを使用できます。

public class MyThread implements Runnable { 
    private List list; 

    public MyThread(List list) { 
     this.list = list; 
    } 

    public static synchronized void writeToFile() { 
     // WRITE TO FILE HERE 
    } 

    @Override 
    public void run() { 
     // DO SOMETHING HERE WITH LIST 
     ... 
     MyThread.writeToFile(); 
     ... 
    } 
} 

new MyThread(list).start(); 
+0

'Thread'を直接拡張するべきではないことを除いて、' new Thread(new Runnable(){/ * This block * /})。start(); 'が良いでしょう。 –

+0

スレッドは同じファイルに書き込みを行っていますが、このように競合することはありませんか? – Wboy

+0

同じファイルに書き込む部分で 'Synchronization'を使用する必要があります –

0

全体を行う、あなたが行う必要があります。

  • スタートすべてのスレッドは、以下の

を終了した後、ループ

  • 書き込み出力ファイルのスレッド例です

  • +0

    このコードには多くのエラーがあります( '最後のlistOfPlugins'行には識別子の予想されるエラーがありますが、タイプを定義すると他のエラーがスローされます。ブロックは閉じておらず、同期化された行は '静的変数は非静的コンテキストからアクセスできません'というエラーを示しています。 – Wboy

    関連する問題