execute Thread.interrupt() Object.notify() at the same time, why does has two results?











up vote
2
down vote

favorite












public class WaitNotifyAll {
private static volatile 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("threadA begin wait");
resourceA.wait();
System.out.println("threadA end wait");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});

Thread threaB = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resourceA) {
System.out.println("threadC begin notify");
threadA.interrupt();
resourceA.notify();
}
}
});

threadA.start();

Thread.sleep(1000);

threaB.start();

System.out.println("main over");
}
}


There are two possible result here:




  1. throws InterruptedException


  2. normal termination



why?



I don't understand. when threadA is interruptted ,result should throws InterruptedException. but sometimes execute this program, it can normal finish.



environment: java8, mac










share|improve this question




























    up vote
    2
    down vote

    favorite












    public class WaitNotifyAll {
    private static volatile 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("threadA begin wait");
    resourceA.wait();
    System.out.println("threadA end wait");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    });

    Thread threaB = new Thread(new Runnable() {
    @Override
    public void run() {
    synchronized (resourceA) {
    System.out.println("threadC begin notify");
    threadA.interrupt();
    resourceA.notify();
    }
    }
    });

    threadA.start();

    Thread.sleep(1000);

    threaB.start();

    System.out.println("main over");
    }
    }


    There are two possible result here:




    1. throws InterruptedException


    2. normal termination



    why?



    I don't understand. when threadA is interruptted ,result should throws InterruptedException. but sometimes execute this program, it can normal finish.



    environment: java8, mac










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      public class WaitNotifyAll {
      private static volatile 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("threadA begin wait");
      resourceA.wait();
      System.out.println("threadA end wait");
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      }
      }
      });

      Thread threaB = new Thread(new Runnable() {
      @Override
      public void run() {
      synchronized (resourceA) {
      System.out.println("threadC begin notify");
      threadA.interrupt();
      resourceA.notify();
      }
      }
      });

      threadA.start();

      Thread.sleep(1000);

      threaB.start();

      System.out.println("main over");
      }
      }


      There are two possible result here:




      1. throws InterruptedException


      2. normal termination



      why?



      I don't understand. when threadA is interruptted ,result should throws InterruptedException. but sometimes execute this program, it can normal finish.



      environment: java8, mac










      share|improve this question















      public class WaitNotifyAll {
      private static volatile 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("threadA begin wait");
      resourceA.wait();
      System.out.println("threadA end wait");
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      }
      }
      });

      Thread threaB = new Thread(new Runnable() {
      @Override
      public void run() {
      synchronized (resourceA) {
      System.out.println("threadC begin notify");
      threadA.interrupt();
      resourceA.notify();
      }
      }
      });

      threadA.start();

      Thread.sleep(1000);

      threaB.start();

      System.out.println("main over");
      }
      }


      There are two possible result here:




      1. throws InterruptedException


      2. normal termination



      why?



      I don't understand. when threadA is interruptted ,result should throws InterruptedException. but sometimes execute this program, it can normal finish.



      environment: java8, mac







      java concurrency interrupt notify






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 15:23









      JB Nizet

      531k51855989




      531k51855989










      asked Nov 11 at 15:22









      zhenyu wu

      212




      212
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Because of reordering. In normal termination compiler reordered instruction interrupt and notify, interruption invokes on working thread and no interrupted exception throws.
          Try to forbid reordering with reading volatile variable and you always get interrupted exception.



          public class WaitNotifyAll {
          private static volatile 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("threadA begin wait");
          resourceA.wait();
          System.out.println("threadA end wait");
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          }
          });

          Thread threaB = new Thread(new Runnable() {
          @Override
          public void run() {
          synchronized (resourceA) {
          System.out.println("threadC begin notify");
          threadA.interrupt();
          System.out.print(resourceA);
          resourceA.notify();
          }
          }
          });

          threadA.start();

          Thread.sleep(1000);

          threaB.start();

          System.out.println("main over");
          }


          }






          share|improve this answer










          New contributor




          eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

























            up vote
            0
            down vote













            When a thread receives both an interrupt and a notify, the behaviour may vary.



            Please refer to https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3



            Credit - Alex Otenko on the Concurrency Interest mailing list






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250167%2fexecute-thread-interrupt-object-notify-at-the-same-time-why-does-has-two-re%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              Because of reordering. In normal termination compiler reordered instruction interrupt and notify, interruption invokes on working thread and no interrupted exception throws.
              Try to forbid reordering with reading volatile variable and you always get interrupted exception.



              public class WaitNotifyAll {
              private static volatile 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("threadA begin wait");
              resourceA.wait();
              System.out.println("threadA end wait");
              } catch (InterruptedException e) {
              e.printStackTrace();
              }
              }
              }
              });

              Thread threaB = new Thread(new Runnable() {
              @Override
              public void run() {
              synchronized (resourceA) {
              System.out.println("threadC begin notify");
              threadA.interrupt();
              System.out.print(resourceA);
              resourceA.notify();
              }
              }
              });

              threadA.start();

              Thread.sleep(1000);

              threaB.start();

              System.out.println("main over");
              }


              }






              share|improve this answer










              New contributor




              eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                0
                down vote













                Because of reordering. In normal termination compiler reordered instruction interrupt and notify, interruption invokes on working thread and no interrupted exception throws.
                Try to forbid reordering with reading volatile variable and you always get interrupted exception.



                public class WaitNotifyAll {
                private static volatile 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("threadA begin wait");
                resourceA.wait();
                System.out.println("threadA end wait");
                } catch (InterruptedException e) {
                e.printStackTrace();
                }
                }
                }
                });

                Thread threaB = new Thread(new Runnable() {
                @Override
                public void run() {
                synchronized (resourceA) {
                System.out.println("threadC begin notify");
                threadA.interrupt();
                System.out.print(resourceA);
                resourceA.notify();
                }
                }
                });

                threadA.start();

                Thread.sleep(1000);

                threaB.start();

                System.out.println("main over");
                }


                }






                share|improve this answer










                New contributor




                eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Because of reordering. In normal termination compiler reordered instruction interrupt and notify, interruption invokes on working thread and no interrupted exception throws.
                  Try to forbid reordering with reading volatile variable and you always get interrupted exception.



                  public class WaitNotifyAll {
                  private static volatile 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("threadA begin wait");
                  resourceA.wait();
                  System.out.println("threadA end wait");
                  } catch (InterruptedException e) {
                  e.printStackTrace();
                  }
                  }
                  }
                  });

                  Thread threaB = new Thread(new Runnable() {
                  @Override
                  public void run() {
                  synchronized (resourceA) {
                  System.out.println("threadC begin notify");
                  threadA.interrupt();
                  System.out.print(resourceA);
                  resourceA.notify();
                  }
                  }
                  });

                  threadA.start();

                  Thread.sleep(1000);

                  threaB.start();

                  System.out.println("main over");
                  }


                  }






                  share|improve this answer










                  New contributor




                  eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  Because of reordering. In normal termination compiler reordered instruction interrupt and notify, interruption invokes on working thread and no interrupted exception throws.
                  Try to forbid reordering with reading volatile variable and you always get interrupted exception.



                  public class WaitNotifyAll {
                  private static volatile 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("threadA begin wait");
                  resourceA.wait();
                  System.out.println("threadA end wait");
                  } catch (InterruptedException e) {
                  e.printStackTrace();
                  }
                  }
                  }
                  });

                  Thread threaB = new Thread(new Runnable() {
                  @Override
                  public void run() {
                  synchronized (resourceA) {
                  System.out.println("threadC begin notify");
                  threadA.interrupt();
                  System.out.print(resourceA);
                  resourceA.notify();
                  }
                  }
                  });

                  threadA.start();

                  Thread.sleep(1000);

                  threaB.start();

                  System.out.println("main over");
                  }


                  }







                  share|improve this answer










                  New contributor




                  eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago





















                  New contributor




                  eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 2 days ago









                  eSerpentine

                  11




                  11




                  New contributor




                  eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  eSerpentine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.
























                      up vote
                      0
                      down vote













                      When a thread receives both an interrupt and a notify, the behaviour may vary.



                      Please refer to https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3



                      Credit - Alex Otenko on the Concurrency Interest mailing list






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        When a thread receives both an interrupt and a notify, the behaviour may vary.



                        Please refer to https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3



                        Credit - Alex Otenko on the Concurrency Interest mailing list






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          When a thread receives both an interrupt and a notify, the behaviour may vary.



                          Please refer to https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3



                          Credit - Alex Otenko on the Concurrency Interest mailing list






                          share|improve this answer












                          When a thread receives both an interrupt and a notify, the behaviour may vary.



                          Please refer to https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3



                          Credit - Alex Otenko on the Concurrency Interest mailing list







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          Ashutosh A

                          63137




                          63137






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250167%2fexecute-thread-interrupt-object-notify-at-the-same-time-why-does-has-two-re%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine