How do I schedule a task to run at periodic intervals?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    #1

    How do I schedule a task to run at periodic intervals?

    was trying some codes to implement a scheduled task and came up with these codes .
    Code:
    import java.util.*;
    
    class Task extends TimerTask {
    
    int count = 1;
    
    // run is a abstract method that defines task performed at scheduled time.
    public void run() {
        System.out.println(count+" : Mahendra Singh");
        count++;
    }
    
    }
    
    class TaskScheduling {
    
    public static void main(String[] args) { Timer timer = new Timer();
    
       // Schedule to run after every 3 second(3000 millisecond)
       timer.schedule( new Task(), 3000);   
    
    } }
    My output :

    1 : Mahendra Singh

    I expected the compiler to print a series of Mahendra Singh at periodic interval of 3 s but despite waiting for around 15 minutes, I get only one output...How do I solve this out?
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Please see this link for timer class information.
    I don't see a listener here.

    Regards
    Dheeraj Joshi

    Comment

    Working...