title: Multi threading in Java. Countdown and timer both at once
body: <p>Both the timer and the countdown method work when they are run individually. They also work as two individual threads however the priority of the thread sequences is causing the count down and the timer(counting up) not to be accurate. </p>
  
  <p>Here is the sample output:cookie 10
  cookie 9
  cookie 8
  seconds 1
  cookie 7
  seconds 2
  seconds 3
  cookie 6
  seconds 4
  cookie 5
  seconds 5
  cookie 4
  cookie 3
  seconds 6
  cookie 2
  seconds 7
  cookie 1
  seconds 8
  seconds 9
  seconds 10
  seconds 11
  seconds 12
  seconds 13
  seconds 14
  seconds 15
  seconds 16
  seconds 17
  seconds 18
  seconds 19</p>
  
  <p>Once the cookie timer is done the timer should continue as it does however while both timer and countdown are active the threads should keep track of the time in seconds without loosing accuracy. At the moment I am relying on the console to display this counts however I will be programming this in a graphical interface that will show the user the countdown and timer. Perhaps execution of the threads need to alternate between them in order for the timers to progress equally, in addition they should be synchronized so that one can not continue without the other. Any tips on implementation. Thanks.</p>
  
  <p>public class Console {</p>
  
  <pre><code>     long lastTime;
       boolean counting = true;
       boolean cCounting = true;
       long seconds = 0;
       long delta = 0;
       volatile int startNumber = (int) Level.cookieTime / 1000;
      Thread countDown;
      Thread time;
  
      public Console() {
  
          counting = Game.enter;
  
          cookieCountDown();
          timer();
          lastTime = System.currentTimeMillis();
      }
  
      public void timer() {
  
          time = new Thread(new Runnable() {
              @Override
              public void run() {
                  seconds = 0;
  
  
                  while (true) {
                      try {
                          Thread.sleep(0);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                      counting = Game.enter;
  
                      while (counting) {
  
                          long now = System.currentTimeMillis();
                          delta = now - lastTime;
  
                          if (delta &gt;= 1000) {
  
                              delta = 0;
                              lastTime = System.currentTimeMillis();
                              System.out.println("seconds " + seconds); //Print seconds
                              seconds++;
  
                              if (!counting){
                                  System.out.println("stoped"  + counting);
                                  //time.stop();
                                //  return;
                              }
                          }
                      }
                  }
              }
  
          });
          time.start();
  
      }
  
      public void cookieCountDown() {
          countDown = new Thread(new Runnable() {
              @Override
              public void run() {
                  Player.cCounting = true;
  
                  while (startNumber != 0) {
                      startNumber = (int) Level.cookieTime / 1000;
                      cCounting = Game.enter;
  
                      while (startNumber &gt; 0 &amp;&amp; cCounting) {
                          long now = System.currentTimeMillis();
                          delta = now - lastTime;
  
                          if (delta &gt;= 1000) {
                              delta = 0;
                              lastTime = System.currentTimeMillis();
                              System.out.println("cookie " + startNumber);// print countdown;
                              startNumber--;
  
                              if (!Player.cCounting) {
                                  Player.cCounting = true;
                                  return;
                              }
                          }
                      }
                  }
              }
          });
          countDown.start();
  
          if (startNumber == 0 || !Player.cCounting) {
  
              Player.cCounting = true;
              startNumber = (int) Level.cookieTime / 1000;
  
          }
      }
  
  
      public void setCounting(boolean counting) {
          this.counting = counting;
      }
  }
  </code></pre>