sdcyst
sdcyst

Reputation: 1

ReentrantLock unlock wake up LockSupport park

I have a program which have multiple threads share the same lock, just like below:

import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    private static final ReentrantLock lock = new ReentrantLock(false);
    
    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            new Thread(new Runnable() {
                public void run() {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName() + "-start");
                    lock.unlock();
                    LockSupport.park();
                    System.out.println(Thread.currentThread().getName() + "-end");
                }
                
            }).start();
        }
    }
}

In my opinion, the "-end" should never been printed because the call to LockSupport.park will pause the thread. In my testing it occasionally prints the "-end" to the console, and I don't understand why.

the output looks like below:

Thread-7978-start
Thread-7976-end
Thread-7979-start
Thread-7980-start

Why might this be happening?

Upvotes: 0

Views: 173

Answers (0)

Related Questions