web123456

Several methods for Java timed tasks (Thread and Timer, thread pool)

  • import ;  
  • import ;  
  • import ;  
  • public class Task3 {  
  •     public static void main(String[] args) {  
  •         Runnable runnable = new Runnable() {  
  •             public void run() {  
  •                 // task to run goes here  
  •                 ("Hello !!");  
  •             }  
  •         };  
  •         ScheduledExecutorService service = Executors  
  •                 .newSingleThreadScheduledExecutor();  
  •         // The second parameter is the delay time for the first execution, and the third parameter is the interval time for the regular execution  
  •         (runnable, 101, );  
  • //         Create and execute ScheduledFuture enabled after a given delay.
    //           Parameters:
    //            callable - function to be executed
    //         delay - the time to delay execution from now on
    //          unit - the time unit of delay parameters
    // return:
    //         ScheduledFuture that can be used to extract results or cancel
  • (runnable, delay, ); 
  •     }  
  • }