0

Can anyone kindly explain me why Thread.currentThread().interrupt(); is called in Bartender InterruptedException handler block when Customer class has already called it. If I comment that line, 'if (Thread.interrupted()) { ' (in Bartender class's run() method )is never called.

public class Main {
    public static void main(String[] args) {
        Bartender bartender = new Bartender();
        Thread bartenderThread = new Thread(bartender, "Bartender");

        bartenderThread.start();

        // Not very robust, but should allow the bartender to get to sleep first
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            // This can be ignored
        }

        int numCustomers = 1;

        for (int i = 1; i <= numCustomers; i++) {
            String customerName = "Customer " + i;
            Customer customer = new Customer(bartenderThread, customerName, 10);

            new Thread(customer, customerName).start();
        }
    }
}

public class Bartender implements Runnable {
    public void run() {
        System.out.println("Bartender: My boss isn't in today; time for a quick snooze!");

        while (true) {
            if (Thread.interrupted()) { // Check to see if we have been interrupted,
                System.out.println("Bartender: I have to serve customer..");
                System.out.println("Serving customer...........");
                System.out.println("Serving customer DONE");
            }

            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                System.out.println("Bartender Interrupted Exception Handler");
                Thread.currentThread().interrupt();
            }
        }
    }
}

public class Customer implements Runnable {
    private Thread bartenderThread;
    private String name;
    private int waitTime;

    public Customer(Thread bartenderThread, String name, int waitTime) {
        this.bartenderThread = bartenderThread;
        this.name = name;
        this.waitTime = waitTime;
    }

    public void run() {
        System.out.println(name + ": Doesn't seem to be anyone around. I'll wait a moment");

        try {
            TimeUnit.SECONDS.sleep(waitTime);
        } catch (InterruptedException e) {
            // This can be ignored
        }

        System.out.println(name + ": Oh there's a bell! Can I get some service around here?");
        System.out.println("bartenderThread: " + bartenderThread);

        bartenderThread.interrupt(); //
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
vivek4348
  • 435
  • 1
  • 5
  • 15
  • 1
    Read the [Thread API](http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html) section on the `interrupted()` method. This clears the interrupt flag, and it must be reset. – Hovercraft Full Of Eels Jul 24 '17 at 01:08
  • I got it. Thank you. When the sleep() method's InterruptedException is thrown, Thread.interrupted() is called. "InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown." This is the reason why if (Thread.interrupted()) { always returns false. Thank you. – vivek4348 Jul 24 '17 at 01:59

0 Answers0