将参数传递给Java.util.Concurrent中的Call方法

use*_*861 0 java concurrency multithreading java.util.concurrent

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

像这样的东西:

public String call (String param)
Run Code Online (Sandbox Code Playgroud)

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

Vit*_*liy 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
   }
}
Run Code Online (Sandbox Code Playgroud)

这种模式称为"方法对象"