简体   繁体   English

Java从txt文件的特定行读取并将其线程化

[英]Java read from certain line from txt file and thread it

So what i'm wanting to do is read from "UsernameList.txt" and somehow make it do something like this 所以我想从“ UsernameList.txt”中读取内容,并以某种方式使其执行以下操作

Text in the file: 文件中的文本:

construct
Hustle
savior
power
Revenge

Read the first line, connect, while it's reading the second line doing the same thing. 阅读第一行,连接,而阅读第二行做同样的事情。 Is this possible? 这可能吗? and how might this be done? 以及如何做到这一点?

public class main {
    public static void main(String[] args) throws IOException, InterruptedException {
        BufferedReader fileR = new BufferedReader(new FileReader("UsernameList.txt"));

        String line = null;
        while ((line = fileR.readLine()) != null) {
            Document doc = Jsoup.connect("https://twitter.com/" + line).get();
            doc.html();
        }
    }
}

You can do something like this. 你可以做这样的事情。

public class Tester implements Runnable {

    String line = null;

    public static void main(String[] args) throws IOException {
        BufferedReader fileR = new BufferedReader(new FileReader("UsernameList.txt"));
        String line = null;
        while ((line = fileR.readLine()) != null) {
            Tester tester = new Tester();
            tester.line = line;
            Thread thread = new Thread(tester);
            thread.start();
        }

    }

    @Override
    public void run() {
        Document doc = Jsoup.connect("https://twitter.com/" + line).get();
        doc.html();
    }
}

You can have threads that implement Callable, which accepts the keywords connects and responds with required data. 您可以具有实现Callable的线程,该线程接受关键字connect并使用所需数据进行响应。 Also you can use Executor framework to control the the number of thread running at a time. 您也可以使用Executor框架来控制一次运行的线程数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM