0

我是高级 Java.util.Concurrent 包的新手,我想做的是使用线程池同时读取多个文本文件。我需要一种将文件名作为参数传递给我的调用方法实现的方法。

像这样的东西:

public String call (String param)

如果有其他方法可以实现这一点,我将感谢您的帮助。

4

1 回答 1

6

实现Runnable接口时,将参数添加为类的成员。并在构造函数中添加该成员的初始化。比从 run 方法使用它。

例如:

class ConcurrentFileReader implements Runnable{
   String fileName;

   public ConcurrentFileReader(String fileName){ 
       this.fileName = fileName; 
   }

   public void run(){
       File f = new File(fileName);
      // whatever
   }
}

这种模式被称为“方法对象”

于 2012-07-21T16:50:16.683 回答