0
    ReentrantLock l = new ReentrantLock(true);
            Reader[] readers = new Reader[2];
    
            for (int i = 0; i < readers.length; i++) {
                readers[i] = new Reader(l);
            }
            for (int i = 0; i < readers.length; i++) {
                readers[i].start();
            }

public class Reader extends Thread {

    private ReentrantLock l;

    public Reader(ReentrantLock l) {
        this.l = l;
    }

    @Override
    public void run() {
        try {
              l.tryLock();
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " i =  " + i);
                Thread.sleep(500);
            }
//            l.unlock(); // although it commented the code not hanged why?
                   } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

according to my understanding to tryLock() Acquires the lock if it is not held by another thread and returns immediately now in my case i have two threads suppose thread_0 get lock now i have two questions : Q1: why thread_1 still enter the critical section after l.tryLock() isn't it locked by thread_0; Q2: isn't it supposes to my code to be hanged because thread_0 doesn't release the lock #thanks advance

javaway
  • 127
  • 2
  • 8
  • You have to check the return value. If `tryLock()` returns false, you didn't acquire the lock. – shmosel Oct 18 '21 at 21:09
  • @shmosel suppose i don't checke and thread_0 come and execute l.tryLock() and it return true then why thread_1 still access my critical section isn't my critical section locked by thread_0 as l.tryLock(); return true – javaway Oct 18 '21 at 21:17
  • @ahmedahmed If your code enters a section without ensuring it holds the lock first, then by definition that section is *not* a critical section. So you don't actually have a critical section because a thread can enter the section without holding the lock if `tryLock` fails. – David Schwartz Oct 18 '21 at 21:20
  • If `tryLock()` blocked until the lock was free it would be no different than `lock()`. – shmosel Oct 18 '21 at 21:30
  • @DavidSchwartz isn't l.tryLock(); if it return true == l.lock(); – javaway Oct 18 '21 at 21:32
  • I think you're misunderstanding how locks work. Calling `lock()` doesn't inherently block other threads from accessing a block of code. It just forces other threads calling `lock()` to wait until you `unlock()`. If one thread locks over a block of code, nothing prevents another thread from accessing the same code without acquiring the same lock. If you call `tryLock()` and it returns false, your thread doesn't have the lock and it's on you to ensure the critical section is not executed. – shmosel Oct 18 '21 at 21:46
  • @ahmedahmed But your code enters the following section even if `tryLock` returns `false`. Since a thread can enter that section without holding the lock, that section is *not* a critical section. – David Schwartz Oct 18 '21 at 22:01
  • @ahmedahmed Why do you think the code would hang? What function would the code hang on? There is no critical section in this code for the reason that I explained. – David Schwartz Oct 18 '21 at 22:10
  • @David Schwartz thank you very much for your help but I was think for example if thread_0 called l.tryLock(); and it return true then it behaves like l.lock() but I understand that when we call l.lock(); one thread get the lock and others wait until they get the lock BUT we call l.tryLock() i must make sure the thread get the lock as the others will not wait – javaway Oct 18 '21 at 22:29
  • @ahmedahmed I'm not really sure I understand what you're saying. But you seem to think this code will hang. What function do you think will hang? The `tryLock` function never hangs. – David Schwartz Oct 18 '21 at 22:42

1 Answers1

2

Your code is entering the critical section because, tryLock() is not a blocking function, it will return true or false immediately, and proceed with the "Critical Section" code snippet below.

In the documentation of tryLock() you can read

If the lock is held by another thread then this method will return immediately with the value false.

So you need to call lock() method, which waits until the lock is released by another reader.

abstractnature
  • 456
  • 2
  • 9