在Java

时间:2017-08-23 04:52:53

标签: java multithreading

我熟悉python和ruby中的线程,但是我对如何用Java执行它感到有点迷失。

我见过的例子告诉我一些关于Runnable界面的事情,但我不太确定这是如何解决我正在尝试做的事情。

基本上我正在尝试重构网络刮刀以改为使用线程(因此每个网址都由新线程完成)以加快速度。

在ruby中,我只是做一个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;
    }
}

编辑:更新代码

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";
     }
}

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

总的来说,你应该这样做:

  • 启动线程for for循环
  • 在所有线程完成后写入输出文件

以下是一个例子

public static void main(String[] args) throws FileNotFoundException, IOException
{
    FileReader fr = new FileReader("plugin_list.txt");
    BufferedReader br = new BufferedReader(fr);
    final StringBuffer sb = new StringBuffer();
    final int counter = 0;

    List<String> listOfPlugins = new ArrayList <String>();
    final 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");
         final String astemp = listOfPlugins.get(i).replace("", "");

         new Thread(new Runnable() {
             @Override
             public void run() {
                 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();
                    }
                    sb.append("[PluginName:{"+ astemp +"},"+ "Description:{"+ context + "}]\n");
                    synchronized(Collecting_Description.this) {
                       counter++;
                       // Write output util all threads are finished
                       if (counter >= listOfPlugins.size()) {
                           FileWriter fw = new FileWriter("DescriptionOutPut.txt",true);
                           BufferedWriter bw = new BufferedWriter (fw);
                           bw.write(sb.toString());
                           bw.close();
                       }
                    }
                 }
                 catch(Exception e)
                 {

                 }

             }
         ).start();


    }
}