| 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.
|
 |
 |
|
|
subject: Threads and Recursion
|
|
|