aspose file tools*
The moose likes Threads and Synchronization and the fly likes Regarding stopping a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Regarding stopping a thread" Watch "Regarding stopping a thread" New topic
Author

Regarding stopping a thread

saxena neera
Greenhorn

Joined: Apr 21, 2012
Posts: 10
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
saxena neera
Greenhorn

Joined: Apr 21, 2012
Posts: 10
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.
 
 
subject: Regarding stopping a thread