4 Replies - 417 Views - Last Post: 28 March 2010 - 11:02 AM Rate Topic: -----

#1 Guest_Clive*


Reputation:

Timer

Posted 28 March 2010 - 09:37 AM

Argggh! Tried everything for this to work.

Ok I have a timer method, which starts at say 30 seconds, once it hits 00:00:00 it says Time is up and the boolean variable finished is set to false!

This works great and when I run it it does what its intended.

However I have a method where I want the user to input words so long as the timer isn't finished. The timer will start but it never countsdown to zero when I put in the other method like so;

		
		Timer timer = new Timer(00,00,05);
		timer.start();
		
		
		System.out.println("Start : " + timer.getTime());	
		
		while(Timer.finished){
			timer.countdown();
			v.inputwords();;
			
						
			
			try {
				Thread.sleep(Timer.ONE_SECONDS);
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		
		}


Its as if the countdown is waiting on the inputwords method to terminate(which it won't) where as the countdown method should be the one deciding when the inputwords one terminates!

Really need help

Is This A Good Question/Topic? 0

Replies To: Timer

#2 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Timer

Posted 28 March 2010 - 10:13 AM

I'd like to see the rest of your code and I haven't worked with threads in a long time but it looks like you are asking for the input words in the same thread as the timer, hence why it stops. You probably want something more like this(keep in mind I haven't run this and wrote it in this window):

    
                Timer timer = new Timer(00,00,05);
                System.out.println("Start : " + timer.getTime());                    
                timer.startCount();             
                
                while(!timer.finished()){
                        v.inputwords();  
                }



and timer would look something like

public class Timer implements Runnable{
    boolean finished;
    int time;
    Thread t;
    public Timer(...args...){
       finished = false;
       time = // arg for time //;
       t = new Thread(this);
    }
    public boolean finished(){
      return finished;
    }

    public void startCount(){
       t.start();
    }

    public boolean getTime(){
      return time;
    }
    public void run(){
      try {
         for(int i = time; i > 0; i--) {
            // Let the thread sleep for a while.
            Thread.sleep(1000);
         }
         finished = true;
     } catch (InterruptedException e) {
         System.out.println("interrupted");
     }
   }
}




May be some other functions specified by Runnable but they are probably fairly trivial to implement.

This post has been edited by SpeedisaVirus: 28 March 2010 - 10:16 AM

Was This Post Helpful? -1
  • +
  • -

#3 toshiro  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 137
  • Joined: 27-June 09

Re: Timer

Posted 28 March 2010 - 10:14 AM

in your whileLoop, you need to say:
while(!Timer.finished())



You were previously saying that while the time IS finished, countdown. In the fix above, the loop will execute whenever the timer IS NOT finished or at 0:00.00

This post has been edited by toshiro: 28 March 2010 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Timer

Posted 28 March 2010 - 10:20 AM

Are you using javax.swing.Timer? If not, I would suggest using a Swing Timer, as you can tie an ActionListener to it. So if you want your program to no longer recieve input after 30 seconds, simply flip a boolean instance variable, then disable your input device after the loop. So for example:
class TimerDemo{
   boolean continue;
   Timer t;

   public TimerDemo(){
        t = new Timer(300, new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //trigger boolean
             }
        });
        
        continue = true;
        t.start();
        t.setRepeats(false);

       //this loop will continue for 30 seconds
       //until the Timer fires the ActionEvent
       //flipping the boolean and closing your input device
        while(continue){
          //get input
        }  

        //close or disable input device
   }//end constructor
   
   public static void main(String[] args){new TimerDemo();}
}//end class


This post has been edited by macosxnerd101: 28 March 2010 - 10:26 AM

Was This Post Helpful? 1
  • +
  • -

#5 Guest_Clive*


Reputation:

Re: Timer

Posted 28 March 2010 - 11:02 AM

Thanks for all the replies, I ended up putting the Timer call into the inputwords method so that they were running parallel and it seems to have worked.
:bananaman:
Was This Post Helpful? 0

Page 1 of 1