I\'m new to Java and I\'m trying to set a simple timer, I\'m familiar with set_interval, because of experience with JavaScript and ActionScript,
I\'m no
Example as you expected in this link
Timer will be running for ever in our application untill application is closed or When there is no more job will be available to assign or Schedule.
TimerTask -- this is the task which has some functionality which is to be run based on time or duration.
In Timer we will assgin TimerTask to run for particular duration or to start its run at particular duration.
Please Understand how it works then apply with applet or any others
1,The GCTask class extends the TimerTask class and implements the run() method.
2,Within the TimerDemo program, a Timer object and a GCTask object are instantiated.
3, Using the Timer object, the task object is scheduled using the schedule() method of the Timer class to execute after a 5-second delay and then continue to execute every 5 seconds.
4,The infinite while loop within main() instantiates objects of type SimpleObject (whose definition follows) that are immediately available for garbage collection.
import java.util.TimerTask;
public class GCTask extends TimerTask
{
public void run()
{
System.out.println("Running the scheduled task...");
System.gc();
}
}
import java.util.Timer;
public class TimerDemo
{
public static void main(String [] args)
{
Timer timer = new Timer();
GCTask task = new GCTask();
timer.schedule(task, 5000, 5000);
int counter = 1;
while(true)
{
new SimpleObject("Object" + counter++);
try
{
Thread.sleep(500);
}
catch(InterruptedException e) {}
}
}
}
public class SimpleObject
{
private String name;
public SimpleObject(String n)
{
System.out.println("Instantiating " + n);
name = n;
}
public void finalize()
{
System.out.println("*** " + name + " is getting garbage collected ***");
}
}