• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need to run threads with different parameters

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Nirvana C",
your name is not valid as per the Javaranch name conventions. Please refer to the document located here
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...thanks a lot .It clarifies a lot many of my doubts
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. And, erm, sorry to start again, but please reregister.
- Peter
 
reply
    Bookmark Topic Watch Topic
  • New Topic