wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Recursive threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Recursive threads" Watch "Recursive threads" New topic
Author

Recursive threads

Chitra Sundaram
Greenhorn

Joined: Oct 24, 2000
Posts: 18
I have a situation where I read a file and write it into another file; while reading, if I find any links in that I read that file too and write it. So reading and writing is recursive till I don't find any links within that file. Since, reading and scanning the file is CPU intensive, I want to implement threads.
What is wrong in a pseudo code like this:

public class ClassA implements Runnable{
public startThread(){
new Thread("...").start();
}
public void run(){
//read the file line by line
// store all links in an array.
for links stored in the array{
startThread();
}
}
}
I tried it... it does not go into the run method at all... Am I missing anything?
Any help would be appreciated.
Thanks
Chitra
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Your code:
public startThread(){
new Thread("...").start();
}
Simply creates a new Thread object, and not a new instance of ClassA, it should read:
new CLassA("...").start();
I would also reocmmend not starting a new thread on each link, because this means the manner in which you read/write the files will not necessarily be consistent from one run to the next, or from one JVM to another (unless you ste up complex thread directives, but that would probably defeat the purpose). You only need one background thread in this case.
--Mark
hershey@eyeshake.com
Chitra Sundaram
Greenhorn

Joined: Oct 24, 2000
Posts: 18
If I don't use one thread per link, the purpose is yet not served. My program takes a huge amount of time to go through each file, read the strings and do some more manipulation before writing them out. So imagine it doing it for 1000 files!! Any suggestions on how I can do it more effectively?
It is more like attacking a Binary search where each node can further be treated as individual search.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Unless you are running amultiprocessor machine, I doubt you will see much practical benefit. On a single processor, ultimately there is no multithreading, the CPU simply executes one operation at a time.
When you create a multithreaded application, you basically tell the application not to finish the operations (code) in the first thread before running the code in the second thread. Typically, you would start a complex operation (be it IO, calulating Pi, etc), and start it in a new thread. This would allow the main program thread (the GUI thread) to run relatively unimpeaded, letting the user start other operations.
If you wanted to do two things in parallel, say calculate Pi, and try to crack an RSA key, then you probably want to start one new thread for each of them, so you don't have to finish calulating Pi to however many digits before you start working on the RSA problem. (This totals 3 threads, Pi, RSA, and the main application thread.) Note that if the Pi question takes X minutes, and the RSA problem takes Y minutes, running them in multiple threads takes X + Y + Z. Why does it take more time? Because the JVM spend non-zero time switching between threads.
So starting multiple thread to read files won't do it any faster (even on a multiprocessor, the bottleneck becomes the fact that you have only on disk to read--unless you have some pretty complex memory system). You simply wnat to make sure any and all file IO is not in the main thread, otherwise you can't continue to click buttons until its done with the file IO. (Of course, not all programs need to do file IO in a new thread.)
--Mark
hershey@eyeshake.com
 
Don't get me started about those stupid light bulbs.
 
subject: Recursive threads
 
Similar Threads
Recursion
searching a content in a file
Any optimization solution?
Need help with using Greek letters read in from file
threads not misbehaving in 1.5