Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:34:29+00:00 2026-05-28T05:34:29+00:00

I want to replace a syncronized block with a ReentrantLock to support interruption of

  • 0

I want to replace a syncronized block with a ReentrantLock to support interruption of waiting for the lock. For this, I use the lockInterruptibly() method and the idiomatic try/finally block:

private ReentrantLock lock = new ReentrantLock();

try
{
  lock.lockInterruptably();
}
catch( InterruptedException e )
{
  Thread.currentThread.interrupt();
}
finally
{
  lock.unlock();
}

The problem is that the finally ofcourse also happens when the InterruptedException happens. This results in an IllegalMonitorStateException, because the lock is not held by the current thread.

This simple program proves this:

public class LockTest
{
public static void main( String[] args )
{
    System.out.println("START");

    Thread interruptThread = new Thread( new MyRunnable( Thread.currentThread() ) );
    interruptThread.start();
    ReentrantLock lock = new ReentrantLock();

    Thread takeLockThread = new Thread( new TakeLockRunnable( lock ) );
    takeLockThread.start();

    try
    {
        Thread.sleep( 500 );
        System.out.println("Trying to take lock on thread " + Thread.currentThread().getName());
        lock.lockInterruptibly();
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    finally {
        lock.unlock();
    }

    System.out.println( "DONE");
}

private static class MyRunnable implements Runnable
{
    private Thread m_thread;

    private MyRunnable( Thread thread)
    {
        m_thread = thread;
    }

    @Override
    public void run()
    {
        try
        {
            Thread.sleep( 1000 );
        }
        catch (InterruptedException e)
        {
            // ignore
        }
        System.out.println( "Interrupting thread " + m_thread.getName() );
        m_thread.interrupt();
    }
}

private static class TakeLockRunnable implements Runnable
{
    private ReentrantLock m_lock;

    public TakeLockRunnable( ReentrantLock lock )
    {
        m_lock = lock;
    }

    @Override
    public void run()
    {
        try
        {
            System.out.println("Taking lock on thread " + Thread.currentThread().getName());
            m_lock.lock();
            Thread.sleep( 20000 );
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            m_lock.unlock();
        }
    }
}
}

It prints this output:

START
Taking lock on thread Thread-1
Trying to take lock on thread main
java.lang.InterruptedException
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:877)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1201)
    at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:312)
    at LockTest.main(LockTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Exception in thread "main" java.lang.IllegalMonitorStateException
    at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:127)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1239)
    at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
    at LockTest.main(LockTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Interrupting thread main

Any idea on what the best way is to avoid this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:34:29+00:00Added an answer on May 28, 2026 at 5:34 am

    the lockInterruptibly() call should be outside the finally block. note, this is always true when using the Lock API (whether you use lock() or lockInterruptibly()), as you don’t want to do the "unlock" work unless you have acquired the lock.

    try {
      lock.lockInterruptibly();
      try {
        // do locked work here
      } finally {
        lock.unlock();
      }
    } catch( InterruptedException e ) {
      Thread.currentThread.interrupt();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to replace the first context of web/style/clients.html with the java String.replaceFirst method
I Want Replace String ':' by '/', I am Using this code to replace
I have this code, and I want to know, if I can replace only
I want to replace the class with the div text like this : This:
I want to replace the 'client' field text box in fogbugz when you edit
I want to replace RBSplitView with NSSplitView in my existing project. The application is
I want to replace the last String which is a , with ) .
I want to replace and insert escape character sequences with their escape character values,
I want to replace only the first occurrence of a word in a sentence
I want to replace all the occurrences of a dot( . ) in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.