KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Timer

java.util
Class Timer

java.lang.Object
  extended by java.util.Timer
See Also:
Top Examples, Source Code, TimerTask, Object.wait(long)

public void cancel()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[367]Uses java.util.Timer to schedule a task
By Pal on 2005/08/20 00:25:27  Rate
import java.util.Timer; 
 import java.util.TimerTask; 
  
  
 /** 
  * Simple demo that uses java.util.Timer to schedule a task to execute 
  * once 5 seconds have passed. 
  */
 
  
  
 public class Reminder  {  
     Timer timer; 
  
  
     public Reminder ( int seconds )   {  
         timer = new Timer (  ) ; 
         timer.schedule ( new RemindTask (  ) , seconds*1000 ) ; 
      }  
  
  
     class RemindTask extends TimerTask  {  
         public void run (  )   {  
             System.out.println ( "Time's up!" ) ; 
       timer.cancel (  ) ; //Terminate the timer thread 
          }  
      }  
  
  
     public static void main ( String args [  ]  )   {  
   System.out.println ( "About to schedule task." ) ; 
         new Reminder ( 5 ) ; 
   System.out.println ( "Task scheduled." ) ; 
      }  
  }  
 


public int purge()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void schedule(TimerTask task,
                     Date time)
See Also:
IllegalStateException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void schedule(TimerTask task,
                     Date firstTime,
                     long period)
See Also:
IllegalStateException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void schedule(TimerTask task,
                     long delay)
See Also:
IllegalStateException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1461]_
By ramya_vijayakumar { at } infosys { dot } com on 2005/06/24 02:17:56  Rate
SimpleMailer sm=new SimpleMailer (  ) ; 
 


public void schedule(TimerTask task,
                     long delay,
                     long period)
See Also:
IllegalStateException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void scheduleAtFixedRate(TimerTask task,
                                Date firstTime,
                                long period)
See Also:
IllegalStateException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1471]Run a task everyday at 12.00 pm
By sasirega { at } hotmail { dot } com on 2005/07/05 10:45:36  Rate
Hi All, 
   I wanted to run a task everyday at 12.00 pm. So i have given the following code. I want to know if it work perfectly every day by 12.00p.m. 
  
  
 import java.util.*; 
  
  
 public class TimerTest   
  {  
   Timer t; 
   String str; 
    
         public TimerTest ( Date dt,String str1 )  
    {  
     t=new Timer (  ) ; 
     str=str1; 
     TimerTest11 ( dt,t ) ; 
    }  
   public void TimerTest11 ( Date dd,Timer t )  
    {  
     TimerTaskTest ttt=new TimerTaskTest (  ) ; 
  
  
        
     t.scheduleAtFixedRate ( ttt,dd,86400000 ) ; // calculated as 24*60*60*1000 so that the time delay 
             // between each execution will be 24 hrs 
  
  
    }  
  
  
 class TimerTaskTest extends TimerTask   
  {  
   public void run (  )  
    {  
     try 
      {  
       System.out.println ( "str................"+str ) ; 
        
        //t.cancel (  ) ; 
      }  
     catch  ( Exception ee )  
      {  
      }  
      
    }  
  
  
  }  
   public static void main ( String [  ]  args )   
    {  
     //TimerTaskTest ttt=new TimerTaskTest (  ) ; 
      
     System.out.println ( "inside main" ) ; 
     Calendar cal=Calendar.getInstance (  ) ; 
     cal.set ( Calendar.HOUR_OF_DAY,12 ) ; 
     cal.set ( Calendar.MINUTE,00 ) ; 
     Date dt=cal.getTime (  ) ; 
      
     System.out.println ( dt ) ; 
     TimerTest t1=new TimerTest ( dt,"i am inside TimerTaskTest" ) ; 
    }  
    
  }  
 


public void scheduleAtFixedRate(TimerTask task,
                                long delay,
                                long period)
See Also:
IllegalStateException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Timer()
See Also:
cancel(), Thread
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Timer(boolean isDaemon)
See Also:
cancel(), Thread
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Timer(String name)
See Also:
Thread.isDaemon(), Thread.getName(), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Timer(String name,
             boolean isDaemon)
See Also:
Thread.isDaemon(), Thread.getName(), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1740]let me know reason
By dseshanna { at } yahoo { dot } co { dot } in on 2006/04/03 22:06:08  Rate
Hi my name is seshu,i am facing one problem in my application i  
  
  
 scheduled to process one task,But this task is going to process one 
  
  
 hour early and it will be in proceess mode for one hour even if it is  
  
  
 stopped...for example our process should start at 10PM but by default i 
  
  
 it will start 9PM only...and this process should get stop at 2AM,but it  
  
  
 is getting stop afet 3.15AM...so please let me know what could be the 
 problem.. 
  
  
  
 thanks 
 seshu 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 

Popular Tags