Results 1 to 5 of 5
  1. #1
    PaulT is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default Application automatically terminates

    Hi,

    I'm an experienced developer, however I'm new to java development.

    At this moment I'm working on a small java application that uses a timer. When I run my application as a Java Application in Eclipse, it will almost immediately terminate. When I call as System.out.println("Hello world!"); in the constructor, this will be executed, so i'm sure my application started running.

    Is this expected behavior? Can I prevent this from happening?

    Any help would be very much appreciated!

    Paul

  2. #2
    mrrmckenzie is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    Try posting your code so we can see what you are doing.

  3. #3
    PaulT is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    I've replicated the problem in the following DemoTermination class:

    Java Code:
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class DemoTermination {
    	private Timer testTimer;
    	
    	public static void main(String[] args)
    	{
    		DemoTermination demo = new DemoTermination();
    	}
    	
    	public DemoTermination(){
    		System.out.println("Hello world!" );
    		testTimer = new Timer("testTimer", true);
    		testTimer.scheduleAtFixedRate( new TestTimer(), 0, 5000 );
    	}
    	
    	protected class TestTimer extends TimerTask {
    		public void run() 
    		{
    			System.out.println("Running timer" );
    			
    		}
    	}
    }
    "Running timer" is only printed once, which is caused by the delay parameter of 0 in this line:
    Java Code:
    testTimer.scheduleAtFixedRate( new TestTimer(), 0, 5000 );
    When I change it from 0 to 5000, it will never be printed.

  4. #4
    mrrmckenzie is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Thumbs up

    Your program only runs the TestTimer once because after the call to the constructor the run method is called when the next line sets up schedule parameters on your timer and then the method finishes. There are no more to execute in the main method so the application then terminates before the next timer event occurs. Try to add
    int i = 0;
    while (i == 0){
    }
    anywhere after your schedule statement. This is just a perpetual loop to demonstrate that if you keep your application alive the timer is actually running.

    Looks like you need to investigate Threads.....
    Last edited by mrrmckenzie; 03-17-2009 at 03:47 AM.

  5. #5
    PaulT is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks, keeping the thread alive by executing this loop works.

Similar Threads

  1. refreshing the page automatically if the configuration file changes
    By anitha2324 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-07-2009, 04:08 PM
  2. Replies: 4
    Last Post: 09-07-2008, 04:58 PM
  3. Automatically File Upload to server ...Help Needed
    By 82rathi.angara in forum New To Java
    Replies: 6
    Last Post: 08-05-2008, 08:13 AM
  4. Replies: 2
    Last Post: 07-04-2008, 01:33 PM
  5. Call a Method Automatically
    By rhm54 in forum New To Java
    Replies: 4
    Last Post: 02-07-2008, 09:51 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
  •