Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interrupt a synchronized method that is blocked

I have an Object with a synchronized method:

public class Foo {
    public synchronized void bar() {
        // Do stuff
    }
}

And I have thousands of threads calling the same method. When I want to quit the program, how can I interrupt these waiting threads so that the program exits immediately?

I have tried to call Thread.interrupt() and Foo.notify() but not work.

The question is: Is blocking synchronized method interruptible?

like image 302
whitecoffee Avatar asked Jul 17 '14 04:07

whitecoffee


People also ask

What can be done to interrupt a synchronized method that is blocked?

In case of synchronized keyword, a thread can be blocked waiting for lock, for an indefinite period of time and there was no way to control that. ReentrantLock provides a method called lockInterruptibly(), which can be used to interrupt thread when it is waiting for lock.

Can a blocked thread be interrupted?

A blocked thread can be interrupted by calling the interrupt() method of Thread class. This interrupt is a pure Java mechanism and is neither CPU nor operating system level interrupt. The interrupt() method does not interrupt a running thread i.e. thread which is in RUNNABLE state.

Can a block be synchronized?

A Synchronized block is a piece of code that can be used to perform synchronization on any specific resource of the method. A Synchronized block is used to lock an object for any shared resource and the scope of a synchronized block is smaller than the synchronized method.

How do I stop synchronization in Java?

Method 1 modifying the variable a and method 2 modifying the variable b, the concurrent modification of the same variable by two threads should be avoided and it is. BUT while thread1 modifying a and thread2 modifying b it can be performed without any race condition.


1 Answers

Is blocking synchronized method interruptible? NO ,but below is the best way to achieve what you wanted to do!

public class Foo {
    private final  Lock lock  = new ReentrantLock();
    public void bar() throws InterruptedException {
        lock.lockInterruptibly();
        try {
          // Do stuff
        }finally {
           lock.unlock()
        }
    }
}

Please use java.util.concurrent.locks.Lock for this purpose. From Java doc of lockInterruptibly method

/**
     * Acquires the lock unless the current thread is
     * {@linkplain Thread#interrupt interrupted}.
     *
     * <p>Acquires the lock if it is available and returns immediately.
     *
     * <p>If the lock is not available then the current thread becomes
     * disabled for thread scheduling purposes and lies dormant until
     * one of two things happens:
     *
     * <ul>
     * <li>The lock is acquired by the current thread; or
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
     * current thread, and interruption of lock acquisition is supported.
     * </ul>
     *
     * <p>If the current thread:
     * <ul>
     * <li>has its interrupted status set on entry to this method; or
     * <li>is {@linkplain Thread#interrupt interrupted} while acquiring the
     * lock, and interruption of lock acquisition is supported,
     * </ul>
     * then {@link InterruptedException} is thrown and the current thread's
     * interrupted status is cleared.

Reference : http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/concurrent/locks/ReentrantLock.java#ReentrantLock.lockInterruptibly%28%29

like image 133
veritas Avatar answered Oct 05 '22 22:10

veritas