私は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";
}
}
'Thread'を直接拡張するべきではないことを除いて、' new Thread(new Runnable(){/ * This block * /})。start(); 'が良いでしょう。 –
スレッドは同じファイルに書き込みを行っていますが、このように競合することはありませんか? – Wboy
同じファイルに書き込む部分で 'Synchronization'を使用する必要があります –