Results 1 to 5 of 5

Thread: Error while using the timer class in Java

  1. #1
    Join Date
    Dec 2009
    Posts
    68

    Error while using the timer class in Java

    Hello I am learning to use the timer class but I have a small problem in a very simple program that aims to display a number incremented every second up to 10:
    Code:
    import java.util .*;
    
     Print public class (
    
    	
                public static void main (String [] args) (
    
    		 Timer timer;
    		 int delay = 1000;
    		 int nb = 10, inc = 1;
    		
    		
    		 timer = new Timer (delay, this);
    		 while (nb <= 10)
    		 (
    			 timer. start ();
    			 System. Out. System.out.println (inc);
    			 inc + +;
    		 )
             )
     )
    That is the problem I have:
    Can not use this in a static context
    The method start () is undefined for the type Timer
    I do not understand very well the mistakes I made. Please help me to sort out my problem.!!

  2. #2
    Join Date
    Mar 2008
    Posts
    258

    Re: Error while using the timer class in Java

    timer = new Timer (delay, this);
    Here this term is placed in a static context - the main method - and thus there is no current instance, so no this. I assume you have a swing timer, to solve the problem would therefore need you to straighten out the ActionListener interface on your class Print, and instead of passing this, pass an instance of this class.

  3. #3
    Join Date
    Apr 2008
    Posts
    193

    Re: Error while using the timer class in Java

    No this is not a timer that uses swing here, but I an impression he has a bit mixed up different types of timer ... Analogous to each timer object is a single background thread that is used to implement all of the timer's tasks, consecutively. Timer "should complete tasks quickly. If a timer task takes disproportionate time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in hasty succession when (and if) the task offending lastly complete.

  4. #4
    Join Date
    Feb 2009
    Posts
    105

    Re: Error while using the timer class in Java

    After the preceding live reference to a Timer object goes away and all exceptional have completed tasks execution, the timer's task execution thread elegantly lapse (subject to and becomes garbage collection). However, this can take capriciously long to occur. By default, the task execution thread does not run as a daemon thread, so it is competent of keeping from terminating the application year. If a caller wants to terminate a timer's task execution thread quickly, the caller should invoke the timer's cancel method.

  5. #5
    Join Date
    Jan 2009
    Posts
    140

    Re: Error while using the timer class in Java

    I think that you will able to do the coding properly after having a look at the example that explains about the timer class. The following is an example with a java.util.Timer :
    Code:
    Import java.util.Timer;
     Import java.util.TimerTask;
    
     (public class PrintTimer
         private int count = 0;
         private static int NB = 10;
    
         public static void main (String [] args) (
             Final PrintTimer pt = new ();
            
             final Timer t = new Timer ();
             t.schedule (new TimerTask () (
                 public void run () (
                     System. Out. System.out.println (count + + pt);
                     if (count == pt PrintTimer. NB) (
                         t cancel ();
                     )
                 )
             ), 0, 1000);
         )
     )

Similar Threads

  1. Replies: 5
    Last Post: 27-08-2011, 10:36 PM
  2. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  3. Timer and alarm in C / Java
    By Izek in forum Software Development
    Replies: 4
    Last Post: 20-07-2010, 03:24 PM
  4. Problem in java timer Class
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 12:48 AM
  5. How to use Swing Timer class in java program?
    By KALANI84 in forum Software Development
    Replies: 4
    Last Post: 25-01-2010, 04:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,710,814,547.98898 seconds with 17 queries