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){
}
}
}
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.
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.
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.
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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With