user1203861
user1203861

Reputation: 1217

Pass parameter to Call method in Java.util.Concurrent

i am new to the high level Java.util.Concurrent package , what i am trying to do is read multiple text files at the same time using a thread pool. I need a way to pass the file name as an argument to my implementation of the call method .

Something like this :

public String call (String param)

If there is another way to achieve this i will appreciate your help.

Upvotes: 0

Views: 1015

Answers (1)

Vitaliy
Vitaliy

Reputation: 8206

When implementing the Runnable interface, add your parameter as a member of the class. And add an initialization of this member in the constructor. Than use it from the run method.

For example:

class ConcurrentFileReader implements Runnable{
   String fileName;

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

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

This pattern is known as a "Method Object"

Upvotes: 6

Related Questions