title: ReentrantLock doesn't work out
body: <p>I can't figure out why the code doesn't work properly. The problem is that ReentrantLock doesn't lock methods invocation in ThreadClass.run()</p>
  
  <p><strong>Resource-class which methods are assumed to be locked in ThreadClass</strong></p>
  
  <pre><code>public class ResourceClass {
  
      private int i; 
  
      public void setIncrement() {
          i++;
      }
  
      public int getIncrement() {
          return i;
      }
  
  }
  </code></pre>
  
  <p><strong>Thread-class</strong></p>
  
  <pre><code>public class ThreadClass implements Runnable {
  
      private ResourceClass resource;
      private ReentrantLock reentrantLock = new ReentrantLock();
  
      ThreadClass(ResourceClass r) {
          resource = r;
      }
  
      public void run() {
          reentrantLock.lock();
          try {
              resource.setIncrement();
              System.out.println(resource.getIncrement());
          } finally {
              reentrantLock.unlock();
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Main-class</strong></p>
  
  <pre><code>public class MainClass {
  
      public static void main(String[] args) {
  
          ResourceClass resource = new ResourceClass();
  
          Thread thread = new Thread(new ThreadClass(resource));
          thread.start();
          Thread thread2 = new Thread(new ThreadClass(resource));
          thread2.start();
          Thread thread3 = new Thread(new ThreadClass(resource));
          thread3.start();
      }
  
  } 
  </code></pre>
  
  <p>It is supposed that the code surrounded by lock in run() must be "synchronised" thus results that only one thread has access to the methods of Resourse-object. At practice it doesn't. The code results repeated numbers which means that two threads have access to the methods at the same time. I know that it is really simple issue but I can't understand how to solve it. Thanks for assistance.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I got that. That piece of code works just fine (I deleted setIncrement() and put all the relevant logic into getIncrement()):</p>
  
  <pre><code>public int getIncrement() {
          reentrantLock.lock();
          int incrementResult = i++;
          reentrantLock.unlock();
          return incrementResult;
  } 
  </code></pre>