Results 1 to 8 of 8
  1. #1
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Java timer issue not running correctly

    Hello,


    Just looking for some help here.

    I have created a separate timer class that runs once every 60 minutes. This class works fine.


    Java Code:
    public Initialize(int seconds){
    	
    		Timer timer = new Timer();
    		timer.schedule(new timerTask(), 0, 1*600000);
    	
    	}
    	
    	
    	class timerTask extends TimerTask{		
    		public void run(){
    			 Date date = new Date();
    			 try{
    			
    			System.out.println(date);
    	
    			 }catch (Exception e) {
    				 System.out.println(e);
    				 running = false;
    				 //stopping the execution of the thread if their is an error
    				 System.exit(0);
    				 while (running = false){
    					 System.out.println("in the while command");
    					//after killing the thread I now need to restart the timer process thread automatically.
    					 //setting it to restart after 10 minutes
    					 Timer timer = new Timer();
    					
    					 timer.schedule(new timerTask(),600000);
    				 }
    				 
    			} 
    			 running=true;
    		}
    		
    	}
    
    
    	public static void main(String[] args) {
    		new Initialize(0);

    If I update the above code and add the code to implement a new class as per below...it seems to keep looping through the constructor, wont go into the main and wont come out of the constructor.
    It also seems to loose the option to do it only once every 10 minutes.

    Java Code:
    public void run(){
    			 Date date = new Date();
    			 try{
    				 running=true;
    			
    			System.out.println(date);
    			north = new north();
    I need to get it to run through the start element() end element to parse the data.

    Any advice
    Last edited by alexandra12; 08-13-2012 at 09:18 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Java timer issue not running correctly

    while (running = false){
    Should be
    Java Code:
    while (running == false){

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Java timer issue not running correctly

    Quote Originally Posted by quad64bit View Post
    Should be
    Java Code:
    while (running == false){
    No, it should be
    Java Code:
    while (!running) {
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Java timer issue not running correctly

    No, it should be
    Java Code:
    1
    while (!running) {
    Oops, I posted faster than I read :P

  5. #5
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Java timer issue not running correctly

    Quote Originally Posted by DarrylBurke View Post
    No, it should be
    Java Code:
    while (!running) {
    db
    Does that even matter? They are identical statements.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default Re: Java timer issue not running correctly

    You get problems like the above, where the assignment is valid in the statement as it produces a boolean result...so the simple rule is don't do it that way and you'll not end up with silly mistakes like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Java timer issue not running correctly

    Technically OT, but it's certainly bad form to name a variable running and then continue around a while loop while the value of that variable is false.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Java timer issue not running correctly

    Thanks guys,

    I will try that out !

Similar Threads

  1. Timer Class Issue
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 10:31 PM
  2. Need help with Timer running in background
    By Azure in forum New To Java
    Replies: 2
    Last Post: 05-22-2010, 02:08 PM
  3. Java Game Timer Issue! Help
    By smithywill in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2010, 09:09 AM
  4. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 12:25 PM
  5. Running Timer
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 09:24 AM

Posting Permissions

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