fork 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.time.ZoneId;
  8. import java.time.ZonedDateTime;
  9. import java.time.format.DateTimeFormatter;
  10. import java.time.format.FormatStyle;
  11. import java.util.Locale;
  12. import java.util.concurrent.Executors;
  13. import java.util.concurrent.ScheduledExecutorService;
  14. import java.util.concurrent.ScheduledFuture;
  15. import java.util.concurrent.TimeUnit;
  16.  
  17. /* Name of the class has to be "Main" only if the class is public. */
  18. class Ideone
  19. {
  20.  
  21.  
  22. public static void main ( String[] args ) {
  23. Ideone app = new Ideone ();
  24. app.doIt ();
  25. }
  26.  
  27. private void doIt () {
  28. System.out.println ( "INFO - Ideone::doIt - Running." );
  29. BeeperControl bc = new BeeperControl ();
  30. bc.beepRegularly (); // Ask that object to launch the background thread to tell time repeatedly.
  31. try {
  32. Thread.sleep ( TimeUnit.MINUTES.toMillis ( 5 ) ); // Run for five minutes and then shutdown this main thread and the background thread too.
  33. bc.halt (); // Ask that object to stop the background thread.
  34. } catch ( InterruptedException ex ) { // This main thread is either being woken-from-sleep or stopped.
  35. System.out.println ( "INFO - Ideone::doIt - main thread of Ideone app interrupted." );
  36. bc.halt (); // Ask that object to stop the background thread.
  37. }
  38. }
  39.  
  40. class BeeperControl {
  41.  
  42. private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool ( 1 );
  43. private final ZoneId zoneId = ZoneId.of ( "America/Montreal" );
  44. private final Locale locale = Locale.CANADA_FRENCH;
  45. //private final DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.MEDIUM ).withLocale ( this.locale );
  46. private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "hh:mm:ss a" , this.locale );
  47.  
  48. public void beepRegularly () {
  49. // Define task to be performed.
  50. final Runnable beeper = new Runnable () {
  51. public void run () {
  52. try {
  53. ZonedDateTime zdt = ZonedDateTime.now ( zoneId );
  54. System.out.println ( "Now: " + zdt.format ( formatter ) );
  55. } catch ( Exception e ) {
  56. // Always surround your task code with a try-catch, as any uncaught exception causes the scheduler to cease silently.
  57. System.out.println ( "Exception unexpectedly reached 'run' method. " + e.getLocalizedMessage () );
  58. }
  59. }
  60. };
  61. // Start performing that task every so often.
  62. System.out.println ( "INFO - BeeperControl::beepRegularly - Scheduling the executor service to run now. Runs indefinitely." );
  63. final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate ( beeper , 0 , 5 , TimeUnit.SECONDS ); // (Runnable command, long initialDelay, long period, TimeUnit unit)
  64. }
  65.  
  66. public void halt () {
  67. System.out.println ( "INFO - BeeperControl::halt - shutting down the ScheduledExecutorService." );
  68. scheduler.shutdown (); // Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
  69. // scheduler.shutdownNow(); // Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
  70. }
  71. }
  72.  
  73. }
Time limit exceeded #stdin #stdout 5s 1106432KB
stdin
Standard input is empty
stdout
INFO - Ideone::doIt - Running.
INFO - BeeperControl::beepRegularly - Scheduling the executor service to run now. Runs indefinitely.
Now: 04:27:40 AM
Now: 04:27:45 AM
Now: 04:27:50 AM