• 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

Threads and Recursion

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic