Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Stopping a Thread in Infinite Loop

807607Nov 4 2006 — edited Nov 4 2006
I've read several articles on how to stop Threads, and all of them point to using Thread.interrupt(). The problem right now is what happens when the thread is in an infinite loop. For example:
class A implements Runnable
{
    public void run()
    {
        while(!Thread.currentThread().isInterrupted())
              while(true);
    }
}
//in other class's main method:
Thread a = new Thread(new A());
a.start();
a.interrupt();
The a.interrupt() call only sets the isInterrupted flag in Thread, but it does not terminate the thread as a.stop() would. However, stop() throws a ThreadDeath exception that I would not want to have to deal with. Is there any way to stop this infinite loop thread safely?

Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2006
Added on Nov 4 2006
3 comments
1,365 views