This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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..!
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.