title: How do I schedule a task to run once?
body: <p>I want to delay doing something, along the lines of setting a countdown timer that will "do a thing" after a certain amount of time.</p>
  
  <p>I want the rest of my program to keep running while I wait, so I tried making my own <code>Thread</code> that contained a one-minute delay:</p>
  
  <pre><code>public class Scratch {
      private static boolean outOfTime = false;
  
      public static void main(String[] args) {
          Thread countdown = new Thread() {
              @Override
              public void run() {
                  try {
                      // wait a while
                      System.out.println("Starting one-minute countdown now...");
                      Thread.sleep(60 * 1000);
  
                      // do the thing
                      outOfTime = true;
                      System.out.println("Out of time!");
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          };
          countdown.start();
  
          while (!outOfTime) {
              try {
                  Thread.sleep(1000);
                  System.out.println("do other stuff here");
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p><br>
  While this worked, more-or-less, it seemed like there should be a better way of doing this.</p>
  
  <p>After some searching, I found a bunch of questions like these but they don't really address what I'm trying to do:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4544197/how-do-i-schedule-a-task-to-run-at-periodic-intervals">How do I schedule a task to run at periodic intervals?</a> </li>
  <li><a href="https://stackoverflow.com/questions/9375882/how-i-can-run-my-timertask-everyday-2-pm">How i can run my TimerTask everyday 2 PM</a></li>
  <li><a href="https://stackoverflow.com/questions/20387881/how-to-run-certain-task-every-day-at-a-particular-time-using-scheduledexecutorse">How to run certain task every day at a particular time using ScheduledExecutorService?</a></li>
  <li><a href="https://stackoverflow.com/questions/11751329/java-execute-task-with-a-number-of-retries-and-a-timeout/">Java execute task with a number of retries and a timeout</a></li>
  </ul>
  
  <p>I don't need anything this complicated; I just want to do a single thing after a certain amount of time while letting the rest of the program still run.</p>
  
  <p>How should I go about scheduling a one-time task to "do a thing"?</p>