fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.math.BigDecimal;
  8. import java.time.Duration;
  9. import java.time.Instant;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.ScheduledExecutorService;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19.  
  20. Runnable runnable = ( ) -> {
  21. System.out.println( "Running the runnable at " + Instant.now() );
  22. };
  23.  
  24. ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
  25. scheduledExecutorService.schedule( runnable , 10 , TimeUnit.SECONDS ); // Wait ten seconds before running the runnable.
  26.  
  27. try
  28. {
  29. Thread.sleep( Duration.ofSeconds( 12 ).toMillis() ); // Wait long enough for the background thread to do its thing.
  30. }
  31. {
  32. e.printStackTrace();
  33. }
  34.  
  35. // Clean-up.
  36. scheduledExecutorService.shutdown();
  37.  
  38. System.out.println( "Done at " + Instant.now() );
  39.  
  40. }
  41. }
Success #stdin #stdout 0.13s 37792KB
stdin
Standard input is empty
stdout
Running the runnable at 2020-11-16T05:16:25.822894Z
Done at 2020-11-16T05:16:27.821346Z