Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to Use Timer in Java

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Post How to Use Timer in Java

    Hi everybody i did a small sample about using Timers in Java it's so simple to use.The main idea that you want to execute simple process every specific time.

    import java.util.Timer;
    import java.util.TimerTask;
     
    public class TimerSample {
     
        public static void main(String[] args) {
            //1- Taking an instance of Timer class.
            Timer timer = new Timer("Printer");
     
            //2- Taking an instance of class contains your repeated method.
            MyTask t = new MyTask();
     
     
            //TimerTask is a class implements Runnable interface so
            //You have to override run method with your certain code black
     
            //Second Parameter is the specified the Starting Time for your timer in
            //MilliSeconds or Date
     
            //Third Parameter is the specified the Period between consecutive
            //calling for the method.
     
            timer.schedule(t, 0, 2000);
     
     
        }
    }
     
    class MyTask extends TimerTask {
        //times member represent calling times.
        private int times = 0;
     
     
        public void run() {
            times++;
            if (times <= 5) {
                System.out.println("I'm alive...");
            } else {
                System.out.println("Timer stops now...");
     
                //Stop Timer.
                this.cancel();
            }
        }
    }

  2. The Following User Says Thank You to neo_2010 For This Useful Post:

    humbleAleph (August 6th, 2013)


  3. #2
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: How to Use Timer in Java

    Thank you for your useful post.

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Use Timer in Java

    I run this program on netbeans. it worked well except that it didn't end on itself. so I added System.exit(0); after this.cancel();

Similar Threads

  1. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM