aspose file tools*
The moose likes Threads and Synchronization and the fly likes need to run threads with different parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "need to run threads with different parameters" Watch "need to run threads with different parameters" New topic
Author

need to run threads with different parameters

Nirvana C
Greenhorn

Joined: Oct 21, 2001
Posts: 3
Hi ,
I have a code similar to this :
--------------------------------------------------
import java.io.*;
public class SomeClass {
public SomeClass() {
}

public static void main(String str[]){
// get the directory name as a command line argument
String dirName = str[0];
String individualfileName ="";
// get an array of all the files in this directory
private File[] files = (new File(dirName)).listFiles();

int n = files.length;
//loop thru the filenames
for(int x=0 ;x < n ; x++)
{
individualfileName =
files[x].getAbsolutePath();
doParsing(individualfileName)
}//end of for
}

public void doParsing(String fileName){
//code to parse this file .....

}
---------------------------------------------
I want to make use of multithreading (Runnable interface) to do the 'doParsing()' method in separate parallel threads (because there are IO related issues in that method ).
But that would mean I have to call this method doParsing() from a run() method , and start the thread using myThread.start() .
What I am not able to figure out , is how to start different threads with different parameters ..fileName , in this case .
Am I missing out on the fundamentals somewhere ..any help will be appreciated .
[This message has been edited by Nirvana C (edited October 23, 2001).]
[This message has been edited by Nirvana C (edited October 23, 2001).]
[This message has been edited by Nirvana C (edited October 23, 2001).]
[This message has been edited by Nirvana C (edited October 23, 2001).]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Your Runnable class will need to hold the state (in this case, the filename) in one way or another. If you use an anonymous Runnable class, it can be implicit, contained in final variables:This will give each file its own Thread calling doParsing.
- Peter
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
"Nirvana C",
your name is not valid as per the Javaranch name conventions. Please refer to the document located here
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi ,
Is this approach a good practice (ie creating a new anonymous
Runnable class )?
What are the implications as far as performance etc is concerned ?
I was thinking about some way to create multiple Threads on one single Object , and running them with different parameters , am I conceptually wrong here ?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Anirvan Chatterjee:
Is this approach a good practice (ie creating a new anonymous
Runnable class )?
I think it is; in my view, it is not different from the anonymous adapter classes that litter Swing programming. But no doubt there are developers who will strongly disagree with me. If you're uncomfortable with it, you can create a ParserRunner inner class:If you don't like inner classes either, ParserRunner would need a reference to the parser object as well. Alternatives would be to let ParserRunner extend Thread and/or to let ParserRunner implement the parsing process itself.
What are the implications as far as performance etc is concerned ?
None. The overhead in creating a few additional objects pales into insignificance besides the overhead of creating a new Thread. If performance is important to you, use a thread pool - a black box that accepts Runnable jobs and uses a configurable number n of threads to execute them.
I was thinking about some way to create multiple Threads on one single Object , and running them with different parameters , am I conceptually wrong here ?
Yes and no. The run() method doesn't take any parameters, so you have to create an object to hold the filename. This can be an adapter class, anonymous or not; it can be a class that extends Thread itself and has a filename member field; it can be a parser class that is Runnable and has a filename member field.
Personally, I would not go for the last option in any case; from a design point of view I would want the parser to be quite independent from the way it's going to be run.
- Peter

[This message has been edited by Peter den Haan (edited October 25, 2001).]
Nirvana C
Greenhorn

Joined: Oct 21, 2001
Posts: 3
...thanks a lot .It clarifies a lot many of my doubts
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You're welcome. And, erm, sorry to start again, but please reregister.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need to run threads with different parameters