Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: threading the Search method

 
Stefan Olson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this will fall under the category of a newbie thread question.
In the contractor assignment, I have a Search button and a Cancel Search button. Only one is enabled at any time. A press of Search will fork a new thread to call a find method on my DatabaseInterface. This call eventually makes its way to the Data class and I have that search done in another Thread.
If the user then presses the Cancel Search button, the a Data class method gets called which kills the previous mentioned search thread by setting a boolean in its thread class. That boolean is part of the loop control test so it's like a polling mechanism, which I think is ugly.
It works (I tested it by creating a larger .db file and adding a sleep), but is what I'm doing bad practice, and if so as I suspect, what is the best way?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12009
216
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefan,
I don't see a problem with the way you are doing this. This is pretty much the way Sun recommend you stop a thread, and it is safe.
Personally I was not as nice as you - if the user started a search on my system, they were stuck waiting till the results came back I didn't loose any marks for that.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic