title: Two counters on different threads counting until one is done
body: <p>Make 2 thread counters - counting from start to end value. The value of the counter should be displayed for each count. One thread is set to count to a smaller value than the other. When one of the threads counts to the setpoint, the other stops and completes its execution.
  This is my task so I made a solution with a AtomicBoolean run = new AtomicBoolean(true); </p>
  
  <pre><code>/**
   * Implements {@link Runnable} and his method {@link Runnable#run} to run until stop is set to true
   * and count is less than maxCount.
   */
  public class Counter implements Runnable {
      private static final Logger logger = LoggerFactory.getLogger(Counter.class);
      private static final String MAX_COUNT_EXCEPTION_MESSAGE = "maxCount should be bigger than zero!";
      private static final int NUMBER_TO_CHECK_MAX_COUNT_VALIDATION = 1;
      private AtomicBoolean run;
      private int count;
      private int maxCount;
  
      /**
       * Constructs a counter with zero count and run equal to true with a specified maxCount.
       *
       * @param maxCount of the counter
       * @throws IllegalArgumentException if maxCount is smaller than 1
       */
      Counter(int maxCount, AtomicBoolean run) {
          if (maxCount &lt; NUMBER_TO_CHECK_MAX_COUNT_VALIDATION) {
              throw new IllegalArgumentException(MAX_COUNT_EXCEPTION_MESSAGE);
          }
          this.maxCount = maxCount;
          this.run = run;
      }
  
      /**
       * Entry point.
       * &lt;p&gt;
       * Runs until run is set to false and count is less than maxCount.
       * &lt;p&gt;
       * On every loop {@link Counter} increment count with 1 and print the count.
       */
      @Override
      public void run() {
          while (count &lt; maxCount &amp;&amp; run.get()) {
              incrementCount();
              logger.info(this + " : " + count);
          }
          run.set(false);
      }
  
      /**
       * Used for obtaining current value for the count.
       *
       * @return count.
       */
      int getCount() {
          return count;
      }
  
      /**
       * Increment the count.
       */
      private void incrementCount() {
          count++;
      }
  }
  
  /**
   * Runs two counters and print the counting for both of them until one reach his maxCount then both are stopped.
   */
  public class RunnerTwoCounters {
      private static final Logger logger = LoggerFactory.getLogger(RunnerTwoCounters.class);
  
      public static void main(String[] args) throws InterruptedException {
          AtomicBoolean run = new AtomicBoolean(true);
          Counter counter1 = new Counter(2, run);
          Thread thread1 = new Thread(counter1);
          Counter counter2 = new Counter(10, run);
          Thread thread2 = new Thread(counter2);
          thread1.start();
          thread2.start();
          thread1.join();
          thread2.join();
  
          logger.info(counter1 + " " + counter1.getCount());
          logger.info(counter2 + " " + counter2.getCount());
      }
  }
  </code></pre>
  
  <p>How can I make the same thing to work but without Atomicboolean just to work with Object lock and some synchronization on it?</p>