title: ReentrantLock lock and unlock by always the same thread
body: <p>I am trying to implement Reentrant locks on multi-threads but for some reason, the same thread unlocks and then locks again resulting in always the same thread to run therefore the same operation. </p>
  
  <p>Below is the code how the threads are spawn</p>
  
  <pre><code>IntStream.range(0,(NUMBER_OF_THREADS)).forEach(index -&gt;{
              boolean operation = (index % 2 == 0) ? true : false;
              Thread t = new Thread(new Client(operation,this));
              t.start();
  });
  </code></pre>
  
  <p>and here is how the run function of the thread works</p>
  
  <pre><code>@Override
  public void run() {
      while(!Thread.interrupted()) {
          System.out.println("Trying to acquire lock : " + main.getLock().tryLock()
                  + " thread id " + Thread.currentThread().getName());
         // if (main.getLock().tryLock()) {
          try {
              main.getLock().lock();
              if(main.getLock().isHeldByCurrentThread()) {
                  System.out.println("Lock held by this thread " + main.getLock().isHeldByCurrentThread()
                          + " thread id : " + Thread.currentThread().getName());
                  if (operation) {
                      main.getcAaccount().deposit(1);
                  } else {
                      main.getcAaccount().withdraw(2);
                  }
                  Thread.currentThread().sleep(3000);
              }
          } catch (InterruptedException e) {
                  e.printStackTrace();
          } finally {
              System.out.println("Thread id : " + Thread.currentThread().getName() + " unlocking");
              main.getLock().unlock();//always have the unlock part here to ensure it unlock
          }
  }
  </code></pre>
  
  <p>It correctly prints that the other 5 threads are trying to acquire the lock and failing and then Thread id...is unlocking...and immediately the same thread locks again even though it should be sleeping. </p>
  
  <p>Have I missed anything in this logic scenario?</p>
  
  <p>Thank you in advance. </p>
  
  <p>EDIT Screenshot of the suggested fix.
  <a href="https://i.stack.imgur.com/JzaSV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JzaSV.png" alt="Still, same thread id unlocks and locks immediately"></a></p>