Actual use of lockInterruptibly for a ReentrantLock










10















What do you actually use for this method lockInterruptibly? I have read the API however it's not very clear to me. Could anybody express it in other words?










share|improve this question


























    10















    What do you actually use for this method lockInterruptibly? I have read the API however it's not very clear to me. Could anybody express it in other words?










    share|improve this question
























      10












      10








      10


      4






      What do you actually use for this method lockInterruptibly? I have read the API however it's not very clear to me. Could anybody express it in other words?










      share|improve this question














      What do you actually use for this method lockInterruptibly? I have read the API however it's not very clear to me. Could anybody express it in other words?







      java concurrency locking reentrantlock






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 23 '13 at 13:26









      RollerballRollerball

      4,6811665120




      4,6811665120






















          3 Answers
          3






          active

          oldest

          votes


















          5














          The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt signal sent to it from another thread.



          How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.






          share|improve this answer























          • As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?

            – MKod
            Dec 21 '14 at 9:40











          • Any thread can interrupt the thread, it doesn't need to be the one holding the lock.

            – Marko Topolnik
            Dec 21 '14 at 20:09











          • Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that, lockInterruptibly continuously tests for them explicitly?

            – overexchange
            Dec 19 '17 at 16:21











          • @overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its interrupted flag, it will be notified and when it resumes, it will throw the InterruptedException

            – Marko Topolnik
            Dec 20 '17 at 9:02


















          7














          lockInterruptibly() may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock(). But if another thread interrupts the waiting thread lockInterruptibly() will throw InterruptedException.






          share|improve this answer






























            0














            Try to understand this concept through below code example.



            Code Sample:



            package codingInterview.thread;

            import java.util.concurrent.locks.ReentrantLock;

            public class MyRentrantlock

            Thread t = new Thread()

            @Override
            public void run()

            ReentrantLock r = new ReentrantLock();
            r.lock();

            System.out.println("lock() : lock count :" + r.getHoldCount());

            interrupt();
            System.out.println("Current thread is intrupted");
            r.tryLock();
            System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
            try
            r.lockInterruptibly();
            System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
            catch (InterruptedException e)
            r.lock();
            System.out.println("Error");
            finally
            r.unlock();


            System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());

            r.unlock();
            System.out.println("lock count :" + r.getHoldCount());
            r.unlock();
            System.out.println("lock count :" + r.getHoldCount());



            ;

            public static void main(String str)
            MyRentrantlock m = new MyRentrantlock();
            m.t.start();

            System.out.println("");





            Output:



            lock() : lock count :1
            Current thread is intrupted
            tryLock() on intrupted thread lock count :2
            Error
            lockInterruptibly() not able to Acqurie lock: lock count :2
            lock count :1
            lock count :0





            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',
              autoActivateHeartbeat: false,
              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%2f17811544%2factual-use-of-lockinterruptibly-for-a-reentrantlock%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt signal sent to it from another thread.



              How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.






              share|improve this answer























              • As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?

                – MKod
                Dec 21 '14 at 9:40











              • Any thread can interrupt the thread, it doesn't need to be the one holding the lock.

                – Marko Topolnik
                Dec 21 '14 at 20:09











              • Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that, lockInterruptibly continuously tests for them explicitly?

                – overexchange
                Dec 19 '17 at 16:21











              • @overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its interrupted flag, it will be notified and when it resumes, it will throw the InterruptedException

                – Marko Topolnik
                Dec 20 '17 at 9:02















              5














              The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt signal sent to it from another thread.



              How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.






              share|improve this answer























              • As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?

                – MKod
                Dec 21 '14 at 9:40











              • Any thread can interrupt the thread, it doesn't need to be the one holding the lock.

                – Marko Topolnik
                Dec 21 '14 at 20:09











              • Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that, lockInterruptibly continuously tests for them explicitly?

                – overexchange
                Dec 19 '17 at 16:21











              • @overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its interrupted flag, it will be notified and when it resumes, it will throw the InterruptedException

                – Marko Topolnik
                Dec 20 '17 at 9:02













              5












              5








              5







              The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt signal sent to it from another thread.



              How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.






              share|improve this answer













              The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt signal sent to it from another thread.



              How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 23 '13 at 13:55









              Marko TopolnikMarko Topolnik

              147k19198325




              147k19198325












              • As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?

                – MKod
                Dec 21 '14 at 9:40











              • Any thread can interrupt the thread, it doesn't need to be the one holding the lock.

                – Marko Topolnik
                Dec 21 '14 at 20:09











              • Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that, lockInterruptibly continuously tests for them explicitly?

                – overexchange
                Dec 19 '17 at 16:21











              • @overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its interrupted flag, it will be notified and when it resumes, it will throw the InterruptedException

                – Marko Topolnik
                Dec 20 '17 at 9:02

















              • As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?

                – MKod
                Dec 21 '14 at 9:40











              • Any thread can interrupt the thread, it doesn't need to be the one holding the lock.

                – Marko Topolnik
                Dec 21 '14 at 20:09











              • Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that, lockInterruptibly continuously tests for them explicitly?

                – overexchange
                Dec 19 '17 at 16:21











              • @overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its interrupted flag, it will be notified and when it resumes, it will throw the InterruptedException

                – Marko Topolnik
                Dec 20 '17 at 9:02
















              As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?

              – MKod
              Dec 21 '14 at 9:40





              As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?

              – MKod
              Dec 21 '14 at 9:40













              Any thread can interrupt the thread, it doesn't need to be the one holding the lock.

              – Marko Topolnik
              Dec 21 '14 at 20:09





              Any thread can interrupt the thread, it doesn't need to be the one holding the lock.

              – Marko Topolnik
              Dec 21 '14 at 20:09













              Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that, lockInterruptibly continuously tests for them explicitly?

              – overexchange
              Dec 19 '17 at 16:21





              Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that, lockInterruptibly continuously tests for them explicitly?

              – overexchange
              Dec 19 '17 at 16:21













              @overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its interrupted flag, it will be notified and when it resumes, it will throw the InterruptedException

              – Marko Topolnik
              Dec 20 '17 at 9:02





              @overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its interrupted flag, it will be notified and when it resumes, it will throw the InterruptedException

              – Marko Topolnik
              Dec 20 '17 at 9:02













              7














              lockInterruptibly() may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock(). But if another thread interrupts the waiting thread lockInterruptibly() will throw InterruptedException.






              share|improve this answer



























                7














                lockInterruptibly() may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock(). But if another thread interrupts the waiting thread lockInterruptibly() will throw InterruptedException.






                share|improve this answer

























                  7












                  7








                  7







                  lockInterruptibly() may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock(). But if another thread interrupts the waiting thread lockInterruptibly() will throw InterruptedException.






                  share|improve this answer













                  lockInterruptibly() may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock(). But if another thread interrupts the waiting thread lockInterruptibly() will throw InterruptedException.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 23 '13 at 13:59









                  Evgeniy DorofeevEvgeniy Dorofeev

                  106k23143223




                  106k23143223





















                      0














                      Try to understand this concept through below code example.



                      Code Sample:



                      package codingInterview.thread;

                      import java.util.concurrent.locks.ReentrantLock;

                      public class MyRentrantlock

                      Thread t = new Thread()

                      @Override
                      public void run()

                      ReentrantLock r = new ReentrantLock();
                      r.lock();

                      System.out.println("lock() : lock count :" + r.getHoldCount());

                      interrupt();
                      System.out.println("Current thread is intrupted");
                      r.tryLock();
                      System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
                      try
                      r.lockInterruptibly();
                      System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
                      catch (InterruptedException e)
                      r.lock();
                      System.out.println("Error");
                      finally
                      r.unlock();


                      System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());

                      r.unlock();
                      System.out.println("lock count :" + r.getHoldCount());
                      r.unlock();
                      System.out.println("lock count :" + r.getHoldCount());



                      ;

                      public static void main(String str)
                      MyRentrantlock m = new MyRentrantlock();
                      m.t.start();

                      System.out.println("");





                      Output:



                      lock() : lock count :1
                      Current thread is intrupted
                      tryLock() on intrupted thread lock count :2
                      Error
                      lockInterruptibly() not able to Acqurie lock: lock count :2
                      lock count :1
                      lock count :0





                      share|improve this answer



























                        0














                        Try to understand this concept through below code example.



                        Code Sample:



                        package codingInterview.thread;

                        import java.util.concurrent.locks.ReentrantLock;

                        public class MyRentrantlock

                        Thread t = new Thread()

                        @Override
                        public void run()

                        ReentrantLock r = new ReentrantLock();
                        r.lock();

                        System.out.println("lock() : lock count :" + r.getHoldCount());

                        interrupt();
                        System.out.println("Current thread is intrupted");
                        r.tryLock();
                        System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
                        try
                        r.lockInterruptibly();
                        System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
                        catch (InterruptedException e)
                        r.lock();
                        System.out.println("Error");
                        finally
                        r.unlock();


                        System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());

                        r.unlock();
                        System.out.println("lock count :" + r.getHoldCount());
                        r.unlock();
                        System.out.println("lock count :" + r.getHoldCount());



                        ;

                        public static void main(String str)
                        MyRentrantlock m = new MyRentrantlock();
                        m.t.start();

                        System.out.println("");





                        Output:



                        lock() : lock count :1
                        Current thread is intrupted
                        tryLock() on intrupted thread lock count :2
                        Error
                        lockInterruptibly() not able to Acqurie lock: lock count :2
                        lock count :1
                        lock count :0





                        share|improve this answer

























                          0












                          0








                          0







                          Try to understand this concept through below code example.



                          Code Sample:



                          package codingInterview.thread;

                          import java.util.concurrent.locks.ReentrantLock;

                          public class MyRentrantlock

                          Thread t = new Thread()

                          @Override
                          public void run()

                          ReentrantLock r = new ReentrantLock();
                          r.lock();

                          System.out.println("lock() : lock count :" + r.getHoldCount());

                          interrupt();
                          System.out.println("Current thread is intrupted");
                          r.tryLock();
                          System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
                          try
                          r.lockInterruptibly();
                          System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
                          catch (InterruptedException e)
                          r.lock();
                          System.out.println("Error");
                          finally
                          r.unlock();


                          System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());

                          r.unlock();
                          System.out.println("lock count :" + r.getHoldCount());
                          r.unlock();
                          System.out.println("lock count :" + r.getHoldCount());



                          ;

                          public static void main(String str)
                          MyRentrantlock m = new MyRentrantlock();
                          m.t.start();

                          System.out.println("");





                          Output:



                          lock() : lock count :1
                          Current thread is intrupted
                          tryLock() on intrupted thread lock count :2
                          Error
                          lockInterruptibly() not able to Acqurie lock: lock count :2
                          lock count :1
                          lock count :0





                          share|improve this answer













                          Try to understand this concept through below code example.



                          Code Sample:



                          package codingInterview.thread;

                          import java.util.concurrent.locks.ReentrantLock;

                          public class MyRentrantlock

                          Thread t = new Thread()

                          @Override
                          public void run()

                          ReentrantLock r = new ReentrantLock();
                          r.lock();

                          System.out.println("lock() : lock count :" + r.getHoldCount());

                          interrupt();
                          System.out.println("Current thread is intrupted");
                          r.tryLock();
                          System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
                          try
                          r.lockInterruptibly();
                          System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
                          catch (InterruptedException e)
                          r.lock();
                          System.out.println("Error");
                          finally
                          r.unlock();


                          System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());

                          r.unlock();
                          System.out.println("lock count :" + r.getHoldCount());
                          r.unlock();
                          System.out.println("lock count :" + r.getHoldCount());



                          ;

                          public static void main(String str)
                          MyRentrantlock m = new MyRentrantlock();
                          m.t.start();

                          System.out.println("");





                          Output:



                          lock() : lock count :1
                          Current thread is intrupted
                          tryLock() on intrupted thread lock count :2
                          Error
                          lockInterruptibly() not able to Acqurie lock: lock count :2
                          lock count :1
                          lock count :0






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 21 '18 at 2:56









                          jatin Goyaljatin Goyal

                          835




                          835



























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17811544%2factual-use-of-lockinterruptibly-for-a-reentrantlock%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

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo