Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and Recursion

 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Thanks for the reply and the excellent suggestion. I believe that this suits my needs.

You've been a big help.
 
Something must be done about this. Let's start by reading this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!