aspose file tools
The moose likes Threads and Synchronization and the fly likes Threads and Recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Threads and Recursion" Watch "Threads and Recursion" New topic
Author

Threads and Recursion

Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
Ok...Time to take a breather...What I am trying to figure out is how to interrupt all threads created by a recursion method. Here is what I have:
Simple File Search:
Run Method:
public void run() {
File dir = new File(path);

File[] files = dir.listFiles(); // avail. files
try {
for(int i = 0; i < files.length; i++) { // iterate through all
try {
if(files[i].isDirectory() == true) { // if its a dir.
// if the name matches the search term
if(files[i].getName().toLowerCase().indexOf(searchFor.toLowerCase()) > - 1)
// add it to the DefaultTableModel (passed to constructor)
dtm.addRow(new Object[] {files[i].getAbsolutePath(), "Folder", Long.toString(files[i].length())});
// Since it is a directory we need to traverse it as well
// Here is the recursion call
(new FileSearchThread(files[i].getAbsolutePath(), searchFor, caseSensitive, dtm)).start();
}
else if(files[i].isFile() == true) { // its a file
if(files[i].getName().toLowerCase().indexOf(searchFor.toLowerCase()) > -1) // if its a match
// add it to the DefaultTableModel
dtm.addRow(new Object[] {files[i].getAbsolutePath(), "File", Long.toString(files[i].length())});
}
}
catch(NullPointerException ex) { }
}
}
catch(NullPointerException ex) { }
}

Now...it works great...the thing is though, In the calling class which exends JFrame and has a button to stop file search, How can it enumerate these threads to cancel them when the button is pressed. Ugh!!!


An egg is a chicken's house!
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Is there a reason you're starting threads recursively within threads in the first place? The average filesystem implementation isn't designed to be involved in lots of concurrent, synchronous operations. But anyway...
Trying to track down a lot of threads to tell them something is usually a lot harder than just having the threads track down you. You could have a simple class that basically looks like this:

Pass the same instance of this class to all of the threads and have them poll it before spawning new threads.
[ December 06, 2003: Message edited by: David Weitzman ]
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
David,
Thanks for the reply and the excellent suggestion. I believe that this suits my needs.

You've been a big help.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Threads and Recursion
 
Similar Threads
forwarding the page
Determining Folder/File Size
recursive function usage in JSP
Class loading and plugin system
getPath(), getAbsolutePath(), getCanonicalPath();??