title: Is there some neat way to make Threads (in Java) wait for theoretical time units as opposed to using Thread.sleep()?
body: <p>Currently working on a university assessment, so I won't share specifics and <em>I'm not asking for any explanation that will help me solve the main problem</em>. I've already solved the problem, but my solution might be considered a little messy.</p>
  <p>Basically, we're working with concurrency and semaphores. There is some shared resource that up to <code>X (where X &gt; 1)</code> number of threads can access at a time and an algorithm which makes it a little more complicated than just acquiring and releasing access. Threads come at a certain time, use the resource for a certain time and then leave. We are to assume that <strong>no time is wasted when arriving, accessing, releasing and leaving the resource</strong>. The goal is to demonstrate that the algorithm we have written works by outputting the times a thread arrives, accesses the resource and leaves for each thread.</p>
  <p>I'm using a semaphore with <code>X</code> number of permits to govern access. And it's all working fine, but I think the way I arrive at the expected output might be a bit janky. Here's something like what I have currently:</p>
  <pre><code>@Override
  public void run() {
      long alive = System.currentTimeMillis();
      try { Thread.sleep(arrivalTime * 1000); }
      catch (InterruptedException e) {} // no interrupts implemented
      long actualArriveTime = System.currentTimeMillis() - alive;
  
      boolean accessed = false;
      while (!accessed) accessed = tryAcquire();
      long actualAccessTime = System.currentTimeMillis() - alive;
  
      try { Thread.sleep(useTime * 1000); }
      catch (InterruptedException e) {} // no interrupts implemented
      release();
      long actualDepartTime = System.currentTimeMillis() - alive;
  
      System.out.println(actualArriveTime);
      System.out.println(actualAccessTime);
      System.out.println(actualDepartTime);
  }
  </code></pre>
  <p>I do it this way because where the expected output might be:</p>
  <pre><code>Thread  Arrival Access  Departure
  A       0       0       3
  B       0       0       5
  C       2       2       6
  ...     ...     ...     ...
  </code></pre>
  <p>My output looks something like:</p>
  <pre><code>Thread  Arrival Access  Departure
  A       0       0       3006
  B       0       0       5008
  C       2       2       6012
  ...     ...     ...     ...
  </code></pre>
  <p>I'm essentially making the time period much larger so that if the computer takes a fews milliseconds to <code>acquire()</code>, for example, it doesn't affect the number much. Then I can round to the nearest second to get the expected output. My algorithm works, but there are issues with this. <strong>A:</strong> It's slow; <strong>B:</strong> With enough threads, the milliseconds of delay may build so that I round to the wrong number.</p>
  <p><strong>I need something more like this:</strong></p>
  <pre><code>public static void main(String[] args) {
      int clock = 0;
      while (threadsWaiting) {
          clock++;
      }
  }
  
  @Override
  public void run() {
      Thread.waitUntil(clock == arrivalTime);
  
      boolean accessed = false;
      while (!accessed) accessed = tryAcquire();
      int accessTime = clock;
  
      int depatureTime = accessTime + useTime;
      Thread.waitUntil(clock == departureTime);
      release();
  
      System.out.println(arrivalTime);
      System.out.println(accessTime);
      System.out.println(departureTime);
  }
  </code></pre>
  <p>Hopefully that's clear. Any help is appreciated.
  Thanks!</p>