• 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

Regarding stopping a thread

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I have developed a program to stop a thread below is the code..



But what I grasp from the above code while debugging is that when I start both the threads x and y, then I called x.setCompleted(); which will set the Boolean value Completed to true , and inside the run() method we are checking the method should not return true so for another thread y it is returning false so the second thread is executing..and the first thread is stopped..!please advise is my understanding is correct..!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saxena neera wrote:Hi Folks,
I have developed a program to stop a thread below is the code..



Do you mean "I found this code on a forum?" If so, please point us to the forum post you found the code at.
 
saxena neera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:

saxena neera wrote:Hi Folks,
I have developed a program to stop a thread below is the code..



Do you mean "I found this code on a forum?" If so, please point us to the forum post you found the code at.



No I have developed it
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in that case, then can you please explain in more detail about what part of your code you are having trouble understanding?

I also have some suggestions:
1) In the comments for the class you should probably not try to reference example code which doesn't exist in the current code file.

2) I would suggest implementing a Runnable, rather than extending Thread. You can search for why this is a good idea.

3) I would suggest you use better names for the stopping. For example, using stop() instead of setCompleted(). Determining if a task is complete or not is an internal process (i.e. in the code provided it would seem like the process is 'complete' when the for loop is done.) What you want to do is stop the task before it is completed from an external source. That sounds more like stop() to me. Note that using stop() in the code provided might be hard to do, since you extend Thread and Thread has a stop() method which is a public final and deprecated method - just one bit of baggage you get from extending a Thread instead of implementing Runnable.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic