Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding stopping a thread

 
saxena neera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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..!
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic