简体   繁体   English

Java用参数创建线程

[英]Java create thread with parameters

I have a program that counts several times a word in the text.我有一个程序可以计算文本中一个单词的数倍。 I want the loop to be in a separate thread.我希望循环在一个单独的线程中。 How can I pass parameters articles and stringToSearch to the thread, or set the global parameters?如何将参数articlesstringToSearch传递给线程,或者设置全局参数?

public class Main {
    public static void main(String[] args)  {
        Scanner s = new Scanner(System.in);
        int numberArticles = s.nextInt();
        ArrayList<Article> articles = new ArrayList<>(); 
        for(int i = 0; i < numberArticles; i++) {
            String articleName = s.nextLine();
            String content = ""; 
            File file = new File(articleName + ".txt"); 
              BufferedReader br;
            try {
                br = new BufferedReader(new FileReader(file));
                  String st; 
                  while ((st = br.readLine()) != null) {
                    content += st; 
                  } 
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } 
            articles.add(new Article(articleName, content));
        }
        String stringToSearch = s.nextLine();
        MyThread myThread = new MyThread();
        myThread.start();
    }
}
public class MyThread extends Thread {
    public void run(){
        for(Article article : articles) {
            int counter = 0;
            String[] words = article.getContent().split(" ");
            for (String word : words) {
                if(word.equals(stringToSearch)) {
                    counter++;
                }
            }
        }
    }
  }

You are extending Thread with your custom class.您正在使用自定义类扩展Thread And you can add any number of any additional properties to that class ( MyThread ).您可以向该类 ( MyThread ) 添加任意数量的任何附加属性。 And you can create a constructor in MyThread to pass all those parameters.您可以在MyThread创建一个构造函数来传递所有这些参数。

Here's an example showing how to pass some values into the constructor for your MyThread class.这是一个示例,展示了如何将一些值传递到MyThread类的构造函数中。 This passes two things into the constructor which then saves them to private members which can then be used within the run() method.这将两件事传递给构造函数,然后将它们保存到私有成员中,然后可以在run()方法中使用这些成员。 I removed most of the other code from your question since it wasn't required for this explanation.我从您的问题中删除了大部分其他代码,因为此解释不需要它。

import java.util.ArrayList;

public class Scratch2 {
    public static void main(String[] args) {
        ArrayList<Article> articles = new ArrayList<>();
        String stringToSearch = "...";

        MyThread myThread = new MyThread(articles, stringToSearch);
        myThread.start();
    }
}

public class MyThread extends Thread {
    private final ArrayList<Article> articles;
    private final String stringToSearch;

    public MyThread(ArrayList<Article> articles, String stringToSearch) {
        this.articles = articles;
        this.stringToSearch = stringToSearch;
    }

    public void run() {
        for (Article article : articles) {
            // ... do things with "stringToSearch"
        }
    }
}

class Article {

    // more stuff here

}

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

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