danny.lesnik
danny.lesnik

Reputation: 18639

Interrupt running thread

I'm just improvising with Thread cancellation using thread interruption. Although in my code both threads are stopped, It looks like I'm not catching InterruptedException I' just wonder why?

Producer:

public class Producer implements Runnable{

    private BlockingQueue<String> queue ;

    public Producer(BlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
            try {

        while (!Thread.currentThread().isInterrupted()){
                queue.put("Hello");
            } 
        }catch (InterruptedException e) {
                System.out.println("Interupting Producer");
                Thread.currentThread().interrupt(); 
        }
    }
}

Consumer:

public class Consumer implements Runnable {

    BlockingQueue<String> queue;

    public Consumer(BlockingQueue<String> queue) {
        super();
        this.queue = queue;
    }

    @Override
    public void run() {

        String s;
        try {
            while (!Thread.currentThread().isInterrupted()) {
                s = queue.take();
                System.out.println(s);
            }
        } catch (InterruptedException e) {
            System.out.println("Consumer Interupted");
            Thread.currentThread().interrupt();
        }
    }
}

and now Main:

public static void main(String[] args) {
    BlockingQueue<String> queue = new LinkedBlockingQueue<String>();

    Thread producerThread = new Thread(new Producer(queue));
    Thread consumerThread = new Thread(new Consumer(queue));
    producerThread.start();
    consumerThread.start();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    } finally {
        producerThread.interrupt();
        consumerThread.interrupt();
    }
}

Although the threads stop, I can't realize why InterruptedException is not cough. It supposed to print interruption message inside catch block but nothing is printed

Upvotes: 2

Views: 1808

Answers (2)

Olivier Catteau
Olivier Catteau

Reputation: 64

Here is en example of interruption :

public class TestThread1 implements Runnable {

public void run() {
    while(Thread.currentThread().isInterrupted() == false) {
        System.out.println("dans la boucle");

        //on simule une courte pause

        for(int k=0; k<100000000; k++);

        System.out.println("Thread isInterrupted = " + Thread.currentThread().isInterrupted());
    }
}

public static void main(String[] args) {
    Thread t = new Thread(new TestThread1());
    t.start();

    //on laisse le temps à l'autre Thread de se lancer
    try {
        Thread.sleep(1000);

    } catch(InterruptedException e) {}

    System.out.println("interruption du thread");
    t.interrupt();
}

}

The result of the execution is :

dans la boucle

Thread isInterrupted = false

dans la boucle

interruption du thread

Thread isInterrupted = true

Upvotes: 1

Ingo
Ingo

Reputation: 36329

You have an unbounded queue, hence neither the producer nor the consumer are ever blocked on the queue. Hence, no operation that could throw InterruptedException is interrupted.

Upvotes: 3

Related Questions