Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is program not terminating?

Tags:

java

timer

swing

I am new to using the Timer class, and so am trying to experiment with it before incorporating it into my project. I am wondering why this program does not terminate when the count reaches 5. The program keeps running even though the condition for the while loop is not satisfied.

package Timer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class demo {

    private static int count;


    public static void main(String[] args) {
        ActionListener executeThis = new ActionListener(){


            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("Hello");
                count++;

            }

        };

        Timer timer = new Timer(500, executeThis);
        timer.setInitialDelay(1000);
        timer.start();

        while(count < 5){

        }
    }

}
like image 917
lb91 Avatar asked Mar 22 '16 09:03

lb91


People also ask

Why is my Java program not terminating?

Threads in thread pool will keep running when main is finished. That's why JVM won't shut down. You need to either use daemon-threads, or shutdown the pool explicitly.

Which method is used to terminate a running program?

exit() method terminates the current JVM running on the system which results in termination of code being executed currently. This method takes status code as an argument.

Which block terminate program on false condition?

The statement block will not be executed when the value of the condition is false, but the block is executed at least once. A while loop is commonly used by the programmers. A do-while loop is used in some cases where the condition needs to the checked the end of loop.


2 Answers

for stoping the timer

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicInteger;


public class Test1 {
    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) {
        ActionListener executeThis = new ActionListener(){


            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("Hello");
                count.getAndIncrement();

            }

        };

        Timer timer = new Timer(500, executeThis);
        timer.setInitialDelay(1000);
        timer.start();

        while(count.get() < 5){

        }
        timer.stop();

    }
}

but i think this will be the correct way to do this

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: Romeo Sheshi 
 * Date: 21/03/16
 * Time: 12:12
 */
public class Test {
    private static AtomicInteger count = new AtomicInteger(0);
    private static Timer timer;
    public static void main(String[] args) {


        ActionListener executeThis = new ActionListener(){


            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("Hello");

                if( count.incrementAndGet()==5){
                   stopTimer();
                }

            }

        };
        timer = new Timer(500, executeThis);
        timer.setInitialDelay(1000);
        startTimer();

        while ( count.get()<5){}

    }

    public static void startTimer(){
        timer.start();
    }

    public static void stopTimer(){
        timer.stop();
    }
}
like image 176
Romeo Sheshi Avatar answered Sep 24 '22 21:09

Romeo Sheshi


You should add:

timer.stop();

In order to stop the timer scheduling.

Another issue is the usage of non atomic variable in multi-threaded ennvironment. You can use AtomicInteger instead.

You can do it inside the actionPerformed method if count == 5.

like image 33
BobTheBuilder Avatar answered Sep 20 '22 21:09

BobTheBuilder