Java 1.5 (5.0) new feature: Thread Schedule : Simple Threads « Threads « Java






Java 1.5 (5.0) new feature: Thread Schedule

Java 1.5 (5.0) new feature: Thread Schedule
 

/*
License for Java 1.5 'Tiger': A Developer's Notebook
     (O'Reilly) example package

Java 1.5 'Tiger': A Developer's Notebook (O'Reilly) 
by Brett McLaughlin and David Flanagan.
ISBN: 0-596-00738-8

You can use the examples and the source code any way you want, but
please include a reference to where it comes from if you use it in
your own products or services. Also note that this software is
provided by the author "as is", with no expressed or implied warranties. 
In no event shall the author be liable for any direct or indirect
damages arising in any way out of the use of this software.
*/



import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.*;

public class ScheduleTester {

  public static void main(String[] args) {
    // Get the scheduler
    ScheduledExecutorService scheduler =
      Executors.newSingleThreadScheduledExecutor();

    // Get a handle, starting now, with a 10 second delay
    final ScheduledFuture<?> timeHandle =
      scheduler.scheduleAtFixedRate(new TimePrinter(System.out), 0, 10, SECONDS);    

    // Schedule the event, and run for 1 hour (60 * 60 seconds)
    scheduler.schedule(new Runnable() {
      public void run() {
        timeHandle.cancel(false);
      }
    }, 60*60, SECONDS);


    /**
     * On some platforms, you'll have to setup this infinite loop to see output
    while (true) { }
     */
  }
}

class TimePrinter implements Runnable {

  private PrintStream out;

  public TimePrinter(PrintStream out) {
    this.out = out;
  }

  public void run() {
    out.printf("Current time: %tr%n", new Date());
  }
}
           
         
  








Related examples in the same category

1.Create a thread by implementing Runnable.
2.Create a thread by extending Thread.
3.Thread ReminderThread Reminder
4.Thread Race DemoThread Race Demo
5.Suggesting when to switch threads with yield()Suggesting when to switch threads with yield()
6.Creating threads with inner classesCreating threads with inner classes
7.The safe way to stop a thread
8.Calling sleep() to wait for a whileCalling sleep() to wait for a while
9.Understanding join()Understanding join()
10.Daemon threads spawn other daemon threadsDaemon threads spawn other daemon threads
11.Daemon threads don't prevent the program from ending.
12.Shows the use of thread priorities.Shows the use of thread priorities.
13.Java new feature: threadingJava new feature: threading
14.Two simple threadsTwo simple threads
15.Simple threads creator Simple threads creator
16.Task
17.SimpleThread using the Runnable interface.SimpleThread using the Runnable interface.
18.Very simple Threading example.Very simple Threading example.
19.Test Override ThreadTest Override Thread
20.Test Override
21.Parallelizing Loops for Multiprocessor Machines
22.Listing All Running Threads in a group
23.Setting priorities on the Thread objects
24.Thread priorities.
25.Use a ThreadGroup.
26.Three Threads TestThree Threads Test