Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    [SOLVED] function not executing with scheduleAtFixedRate

    Hello, I'm trying execute a function repeatedly using scheduleAtFixedRate, however it's not working. my code;

    PHP Code:
        public static void KeepAlive(){
            
              
    TimerTask tasknew = new TimerAction();
              
    Timer t = new Timer(true);
              
              
    // scheduling the task at fixed rate
              
    t.scheduleAtFixedRate(tasknew0100); 
              
              
    System.out.println("execute KeepAlive");
        }
        
        static class 
    TimerAction extends TimerTask {
            public static 
    void main(String[] argsthrows SQLException{
                
    System.out.println("execute TimerAction");

                
    Connect conn Connect.getInstance();
                
    Statement st conn.createStatement();
                
    st.executeQuery("SELECT 1");
            }

            @
    Override
            
    public void run() {
                
    // TODO Auto-generated method stub
                
            
    }
        } 
    "execute KeepAlive" comes up when I execute the KeepAlive() function, but not "execute TimerAction".

    any ideas what i'm doing wrong here?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Execute timer action will never come up. That's in the main method of a nested class which is not run; you need to put it into the run method.

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Thanks! yes thats what I figured out with a bit more testing.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    PHP Code:
        static class TimerAction extends TimerTask {
            public static 
    void KeepAlive() throws SQLException{
                
    System.out.println("execute TimerAction");

                
    Connect conn Connect.getInstance();
                
    Statement st conn.createStatement();
                
    st.executeQuery("SELECT 1");
            }

            @
    Override
            
    public void run() {
                
    // TODO Auto-generated method stub
                
    try {
                    
    KeepAlive();
                } catch (
    SQLException e) {
                    
    // TODO Auto-generated catch block
                    
    e.printStackTrace();
                }
            }
        } 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •