Waiting thread doesn't throw InterruptedException after waking-up

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
Hello,

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

Can anyone please help explain this behaviour?
Env : Java-8 on a Mac

public class WaitNotifyAll {

    private static Object resourceA = new Object();

    public static void main(String[] args) throws Exception {

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    try {
                        System.out.println("Thread-A is about to begin waiting");
                        resourceA.wait();
                        System.out.println("Thread-A came out of waiting state. " +
                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("Thread-B is about to notify");
                    threadA.interrupt();
                    resourceA.notify();
                }
            }
        });

        threadA.start();
        Thread.sleep(1000);
        threadB.start();

        System.out.println("Program over!");
    }
}

Output - 

Thread-A is about to begin waiting
Program over!
Thread-B is about to notify
Thread-A came out of waiting state. Its interrupted status is - true



Regards,
Ashutosh





_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

Alex

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

Hello,

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

Can anyone please help explain this behaviour?
Env : Java-8 on a Mac

public class WaitNotifyAll {

    private static Object resourceA = new Object();

    public static void main(String[] args) throws Exception {

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    try {
                        System.out.println("Thread-A is about to begin waiting");
                        resourceA.wait();
                        System.out.println("Thread-A came out of waiting state. " +
                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("Thread-B is about to notify");
                    threadA.interrupt();
                    resourceA.notify();
                }
            }
        });

        threadA.start();
        Thread.sleep(1000);
        threadB.start();

        System.out.println("Program over!");
    }
}

Output - 

Thread-A is about to begin waiting
Program over!
Thread-B is about to notify
Thread-A came out of waiting state. Its interrupted status is - true



Regards,
Ashutosh




_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list

I would have sworn we had some words about this in the Object.wait docs as it’s come up before.

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex



On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list

On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:

I would have sworn we had some words about this in the Object.wait docs as it’s come up before.

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex



On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
--
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list

Spurious wakeup is not the issue – that’s documented in Object.wait as well.

 

Condition also has this:

“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”

which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.

 

David

 

From: Dr Heinz M. Kabutz <[hidden email]>
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

 

On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:

I would have sworn we had some words about this in the Object.wait docs as it’s come up before.

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

--

Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
It's in the spirit of Java to specify whether interruption or notification wins.
It would be good for the behaviors of Object.wait and AQS.ConditionObject to be consistent.
(but Condition is an interface, and there should be some implementer freedom)
(ConditionObject should probably have some more specs to tighten promised behavior)

On Mon, Dec 3, 2018 at 12:26 PM David Holmes via Concurrency-interest <[hidden email]> wrote:

Spurious wakeup is not the issue – that’s documented in Object.wait as well.

 

Condition also has this:

“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”

which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.

 

David

 

From: Dr Heinz M. Kabutz <[hidden email]>
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

 

On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:

I would have sworn we had some words about this in the Object.wait docs as it’s come up before.

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

--

Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
In reply to this post by JSR166 Concurrency mailing list
Is this what you see in the code or what you see the reproducer do?

The reproducer behaves “as expected”, if you insert Thread.sleep(1000) between interrupt and notify, too. (Note, both are inside the same synchronized, so I am not even sure how wait() can choose one over the other - it can’t exit until the notifier exits, and the exit procedure probably differs depending on whether it is a biased lock or a “fat” lock (promoted due to a sleep))


Alex

On 3 Dec 2018, at 20:24, David Holmes <[hidden email]> wrote:

Spurious wakeup is not the issue – that’s documented in Object.wait as well.
 
Condition also has this:
“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”
which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.
 
David
 
From: Dr Heinz M. Kabutz <[hidden email]> 
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up
 
 
On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:
I would have sworn we had some words about this in the Object.wait docs as it’s come up before. 
 
If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.
 
In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.
 
David
 
From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up
 
That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.
 
If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.
 
Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:
 
Hello,
 
In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 
 
Can anyone please help explain this behaviour?
Env : Java-8 on a Mac
 
public class WaitNotifyAll {
 
    private static Object resourceA = new Object();
 
    public static void main(String[] args) throws Exception {
 
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    try {
                        System.out.println("Thread-A is about to begin waiting");
                        resourceA.wait();
                        System.out.println("Thread-A came out of waiting state. " +
                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
 
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("Thread-B is about to notify");
                    threadA.interrupt();
                    resourceA.notify();
                }
            }
        });
 
        threadA.start();
        Thread.sleep(1000);
        threadB.start();
 
        System.out.println("Program over!");
    }
}
 
Output - 
 
Thread-A is about to begin waiting
Program over!
Thread-B is about to notify
Thread-A came out of waiting state. Its interrupted status is - true
 
 
 
Regards,
Ashutosh
 
 
 
 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list


You can see that it only throws an InterruptedException if it was not notified.

On Mon, Dec 3, 2018 at 1:00 PM Alex Otenko via Concurrency-interest <[hidden email]> wrote:
Is this what you see in the code or what you see the reproducer do?

The reproducer behaves “as expected”, if you insert Thread.sleep(1000) between interrupt and notify, too. (Note, both are inside the same synchronized, so I am not even sure how wait() can choose one over the other - it can’t exit until the notifier exits, and the exit procedure probably differs depending on whether it is a biased lock or a “fat” lock (promoted due to a sleep))


Alex

On 3 Dec 2018, at 20:24, David Holmes <[hidden email]> wrote:

Spurious wakeup is not the issue – that’s documented in Object.wait as well.
 
Condition also has this:
“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”
which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.
 
David
 
From: Dr Heinz M. Kabutz <[hidden email]> 
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up
 
 
On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:
I would have sworn we had some words about this in the Object.wait docs as it’s come up before. 
 
If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.
 
In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.
 
David
 
From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up
 
That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.
 
If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.
 
Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:
 
Hello,
 
In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 
 
Can anyone please help explain this behaviour?
Env : Java-8 on a Mac
 
public class WaitNotifyAll {
 
    private static Object resourceA = new Object();
 
    public static void main(String[] args) throws Exception {
 
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    try {
                        System.out.println("Thread-A is about to begin waiting");
                        resourceA.wait();
                        System.out.println("Thread-A came out of waiting state. " +
                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
 
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("Thread-B is about to notify");
                    threadA.interrupt();
                    resourceA.notify();
                }
            }
        });
 
        threadA.start();
        Thread.sleep(1000);
        threadB.start();
 
        System.out.println("Program over!");
    }
}
 
Output - 
 
Thread-A is about to begin waiting
Program over!
Thread-B is about to notify
Thread-A came out of waiting state. Its interrupted status is - true
 
 
 
Regards,
Ashutosh
 
 
 
 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
In reply to this post by JSR166 Concurrency mailing list

If you’re referring to my earlier comment regarding the hotspot implementation, it was by code inspection. If the thread unlinks itself from the wait-set then there should not be any way for it to be “notified”. If the sleep does not force the IE to be thrown then there must be something I’ve overlooked that prevents the thread from being unlinked.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Tuesday, December 4, 2018 6:59 AM
To: [hidden email]
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

Is this what you see in the code or what you see the reproducer do?

 

The reproducer behaves “as expected”, if you insert Thread.sleep(1000) between interrupt and notify, too. (Note, both are inside the same synchronized, so I am not even sure how wait() can choose one over the other - it can’t exit until the notifier exits, and the exit procedure probably differs depending on whether it is a biased lock or a “fat” lock (promoted due to a sleep))

 

 

Alex



On 3 Dec 2018, at 20:24, David Holmes <[hidden email]> wrote:

 

Spurious wakeup is not the issue – that’s documented in Object.wait as well.

 

Condition also has this:

“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”

which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.

 

David

 

From: Dr Heinz M. Kabutz <[hidden email]> 
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

 

On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:

I would have sworn we had some words about this in the Object.wait docs as it’s come up before. 

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-- 

Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz

 


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
Thread.interrupt() does not synchronize with arbitrary locks, so you can’t guarantee the order in which interrupt() and notify() are observed by the thread. If you can’t guarantee the order, don’t design a program to rely on it.

(I appreciate there is implementation detail, too, but I don’t think there is a statement anywhere requiring interrupt() to appear in the order of synchronization actions)

Alex

On 3 Dec 2018, at 21:11, David Holmes <[hidden email]> wrote:

If you’re referring to my earlier comment regarding the hotspot implementation, it was by code inspection. If the thread unlinks itself from the wait-set then there should not be any way for it to be “notified”. If the sleep does not force the IE to be thrown then there must be something I’ve overlooked that prevents the thread from being unlinked.
 
David
 
From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Tuesday, December 4, 2018 6:59 AM
To: [hidden email]
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up
 
Is this what you see in the code or what you see the reproducer do?
 
The reproducer behaves “as expected”, if you insert Thread.sleep(1000) between interrupt and notify, too. (Note, both are inside the same synchronized, so I am not even sure how wait() can choose one over the other - it can’t exit until the notifier exits, and the exit procedure probably differs depending on whether it is a biased lock or a “fat” lock (promoted due to a sleep))
 
 
Alex


On 3 Dec 2018, at 20:24, David Holmes <[hidden email]> wrote:
 
Spurious wakeup is not the issue – that’s documented in Object.wait as well.
 
Condition also has this:
“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”
which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.
 
David
 
From: Dr Heinz M. Kabutz <[hidden email]> 
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up
 
 
On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:
I would have sworn we had some words about this in the Object.wait docs as it’s come up before. 
 
If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.
 
In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.
 
David
 
From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up
 
That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.
 
If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.
 
Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:
 
Hello,
 
In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 
 
Can anyone please help explain this behaviour?
Env : Java-8 on a Mac
 
public class WaitNotifyAll {
 
    private static Object resourceA = new Object();
 
    public static void main(String[] args) throws Exception {
 
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    try {
                        System.out.println("Thread-A is about to begin waiting");
                        resourceA.wait();
                        System.out.println("Thread-A came out of waiting state. " +
                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
 
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("Thread-B is about to notify");
                    threadA.interrupt();
                    resourceA.notify();
                }
            }
        });
 
        threadA.start();
        Thread.sleep(1000);
        threadB.start();
 
        System.out.println("Program over!");
    }
}
 
Output - 
 
Thread-A is about to begin waiting
Program over!
Thread-B is about to notify
Thread-A came out of waiting state. Its interrupted status is - true
 
 
 
Regards,
Ashutosh
 
 
 
 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
In reply to this post by JSR166 Concurrency mailing list
Actually, a better reference:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3 - and 17.2.4 on interactions of notifications and interruptions.

Alex

On 3 Dec 2018, at 20:38, Martin Buchholz via Concurrency-interest <[hidden email]> wrote:

It's in the spirit of Java to specify whether interruption or notification wins.
It would be good for the behaviors of Object.wait and AQS.ConditionObject to be consistent.
(but Condition is an interface, and there should be some implementer freedom)
(ConditionObject should probably have some more specs to tighten promised behavior)

On Mon, Dec 3, 2018 at 12:26 PM David Holmes via Concurrency-interest <[hidden email]> wrote:

Spurious wakeup is not the issue – that’s documented in Object.wait as well.

 

Condition also has this:

“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”

which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.

 

David

 

From: Dr Heinz M. Kabutz <[hidden email]>
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

 

On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:

I would have sworn we had some words about this in the Object.wait docs as it’s come up before.

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

--

Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list

Ah! That’s where we put it. I knew it was written down somewhere!

 

Thanks,

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Tuesday, December 4, 2018 9:23 AM
To: Martin Buchholz <[hidden email]>
Cc: concurrency-interest <[hidden email]>; David Holmes <[hidden email]>
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

Actually, a better reference:

 

https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3 - and 17.2.4 on interactions of notifications and interruptions.

 

Alex



On 3 Dec 2018, at 20:38, Martin Buchholz via Concurrency-interest <[hidden email]> wrote:

 

It's in the spirit of Java to specify whether interruption or notification wins.

It would be good for the behaviors of Object.wait and AQS.ConditionObject to be consistent.

(but Condition is an interface, and there should be some implementer freedom)

(ConditionObject should probably have some more specs to tighten promised behavior)

 

On Mon, Dec 3, 2018 at 12:26 PM David Holmes via Concurrency-interest <[hidden email]> wrote:

Spurious wakeup is not the issue – that’s documented in Object.wait as well.

 

Condition also has this:

“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”

which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.

 

David

 

From: Dr Heinz M. Kabutz <[hidden email]>
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

 

On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:

I would have sworn we had some words about this in the Object.wait docs as it’s come up before.

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

--

Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Waiting thread doesn't throw InterruptedException after waking-up

JSR166 Concurrency mailing list
Wow, that just exactly explains the issue at hand. Thanks Alex, everyone!

On Tue, 4 Dec 2018 at 7:24 AM, David Holmes via Concurrency-interest <[hidden email]> wrote:

Ah! That’s where we put it. I knew it was written down somewhere!

 

Thanks,

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Tuesday, December 4, 2018 9:23 AM
To: Martin Buchholz <[hidden email]>
Cc: concurrency-interest <[hidden email]>; David Holmes <[hidden email]>


Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

Actually, a better reference:

 

https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3 - and 17.2.4 on interactions of notifications and interruptions.

 

Alex



On 3 Dec 2018, at 20:38, Martin Buchholz via Concurrency-interest <[hidden email]> wrote:

 

It's in the spirit of Java to specify whether interruption or notification wins.

It would be good for the behaviors of Object.wait and AQS.ConditionObject to be consistent.

(but Condition is an interface, and there should be some implementer freedom)

(ConditionObject should probably have some more specs to tighten promised behavior)

 

On Mon, Dec 3, 2018 at 12:26 PM David Holmes via Concurrency-interest <[hidden email]> wrote:

Spurious wakeup is not the issue – that’s documented in Object.wait as well.

 

Condition also has this:

“As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.”

which is closer to the current issue, though suggests favoring interruption – which is the opposite of what wait() does.

 

David

 

From: Dr Heinz M. Kabutz <[hidden email]>
Sent: Monday, December 3, 2018 9:45 PM
To: [hidden email]
Cc: Alex Otenko <[hidden email]>; Ashutosh Agrawal <[hidden email]>; David Holmes <[hidden email]>; [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

 

On Mon, 03 Dec 2018 at 13:35, David Holmes via Concurrency-interest <[hidden email]> wrote:

I would have sworn we had some words about this in the Object.wait docs as it’s come up before.

 

If the thread is both notified and interrupted around the same time then the implementation has a choice as to which reason to report that the wait() ended – with the caveat that notifications must not be lost.

 

In terms of the hotspot implementation, the interrupt unparks the thread and it is then racing with the notify() call. If the notify() is processed before the thread can unlink itself from the wait-queue then it will be seen to be notified. Once it has recorded that it was notified it doesn’t check its interrupt status.

 

David

 

From: Concurrency-interest <[hidden email]> On Behalf Of Alex Otenko via Concurrency-interest
Sent: Monday, December 3, 2018 9:09 PM
To: Ashutosh Agrawal <[hidden email]>
Cc: [hidden email]
Subject: Re: [concurrency-interest] Waiting thread doesn't throw InterruptedException after waking-up

 

That’s the correct behaviour, and is not Mac-specific. Since your thread A received both notify and interrupt, it is OK to prefer notify to interrupt.

 

If you comment out the resourceA.notify() part, you will see it then has no choice but throw InterruptedException.

 

Alex

 

On 3 Dec 2018, at 11:01, Ashutosh Agrawal via Concurrency-interest <[hidden email]> wrote:

 

Hello,

 

In the below program, Thread-A enters wait state and gets interrupted by Thread-B before waking up. However, after waking-up, it does NOT throw an InterruptedException (and its interrupted-status is NOT cleared). 

 

Can anyone please help explain this behaviour?

Env : Java-8 on a Mac

 

public class WaitNotifyAll {

 

    private static Object resourceA = new Object();

 

    public static void main(String[] args) throws Exception {

 

        Thread threadA = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    try {

                        System.out.println("Thread-A is about to begin waiting");

                        resourceA.wait();

                        System.out.println("Thread-A came out of waiting state. " +

                                "Its interrupted status is - " + Thread.currentThread().isInterrupted());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

 

        Thread threadB = new Thread(new Runnable() {

            @Override

            public void run() {

                synchronized (resourceA) {

                    System.out.println("Thread-B is about to notify");

                    threadA.interrupt();

                    resourceA.notify();

                }

            }

        });

 

        threadA.start();

        Thread.sleep(1000);

        threadB.start();

 

        System.out.println("Program over!");

    }

}

 

Output - 

 

Thread-A is about to begin waiting

Program over!

Thread-B is about to notify

Thread-A came out of waiting state. Its interrupted status is - true

 

 

 

Regards,

Ashutosh

 

 

 

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

--

Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun/Oracle Java Champion
JavaOne Rockstar Speaker
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

 

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest