-1

I am stopping the thread execution using Thread.interrupt but the thread execution won't stopped . It's still running .

Example :

Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                    int i = 0;
                    while(i<10000){
                        if(Thread.currentThread().isInterrupted()){
                            System.out.println("Thread Interrupted but it still running");
                        }
                        System.out.println(++i);
                        try {
                            Thread.sleep(2);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
            }
        });
        t.start();
        t.interrupt();

I can't check Thread.isInterrupted() then if thread is interrupted break out of the loop . I can't do this . Here , I am just showing the sample example .

My doubt is Thread.interrupt is only sets interrupted flag only or it really stops the execution .

any help regarding this will be appreciated.

How can I stop the thread execution or kill the running thread?

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
kannanrbk
  • 6,964
  • 13
  • 53
  • 94
  • possible duplicate:http://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java – vishal_aim Dec 10 '12 at 04:38
  • Also bear in mind that in the above code, you do actually need to break out of the loop on detecting an interruption. An interrupt will cause isInterrupted() to return true (once) or cause the sleep to wake up with an InterruptedException, but that's all. If as a result of the interrupt you want to break out of the loop, then you need to explicitly do that. – Neil Coffey Dec 10 '12 at 18:01

4 Answers4

1

Thread.interrupt() will genuinely interrupt an operation that actually checks for that state (either, say, an interruptible I/O operation or else some user code that explicitly checks isInterrupted() as in principle you do in the code you quote).

In the specific example you quote, you need to bear in mind that:

  • each processor core executes in the order of billions of instructions per second;
  • process switching occurs in the order of tens to hundreds of times per second.

In other words, your task of decrementing a counter 10,000 times is something that happens so fast that to all intents and purposes it will barely register as being an "interruptible task". In practice, either all 10,000 decrements will happen before the other thread has chance to call interrupt() or they won't.

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
  • Neil Thank You . I just put the sample scenario . It's not my real scenario i am facing . For your point we want to check `Thread.isInterupted` and stop the execution . – kannanrbk Dec 10 '12 at 05:06
  • So... have a look at the generalities of thread interruption here http://www.javamex.com/tutorials/threads/thread_interruption.shtml Otherwise, unless you can expose the Actual Problem You Are Having, it's a bit difficult to respond...! – Neil Coffey Dec 10 '12 at 05:16
1

If the thread is blocked on wait, sleep or join then InterruptedException is thrown otherwise interrupted status is set.

Interrupted status is cleared if Thread.interrupted is called.

Thread.isInterrupted does not clear the interrupted status.

If the thread's interrupted status is set and this thread calls wait, sleep or join then interrupted status is cleared and InterruptedExcepion is thrown.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

It only sets the flag UNLESS an interruptable statement (e.g. sleep, wait) was executing at the time. Now you should be able to work out how to stop the thread, by dealing with both situations.

I can't check Thread.isInterrupted() then if thread is interrupted break out of the loop . I can't do this .

Why not?

E.g.

while(i<10000){
    if(Thread.currentThread().isInterrupted()){
        System.out.println("Thread Interrupted but it still running");
        return;    // HERE
    }
    System.out.println(++i);
    try {
        Thread.sleep(2);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return;    // HERE
    }
}
xagyg
  • 9,562
  • 2
  • 32
  • 29
0

My doubt is Thread.interrupt is only sets interrupted flag only or it really stops the execution.

It does the former ... or throws an "interrupted" exception in certain circumstances.

How can I stop the thread execution or kill the running thread?

In general, you cannot safely stop or kill a running thread that is not checking its interrupted flag. There are some deprecated methods on the Thread class, but you should avoid using them as they are manifestly unsafe. (To understand why the Thread method javadocs ... and the FAQ that it links to.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216