Java timer not triggering after a random time - java

I have a Java Swing timer which updates a label every second. After starting the timer, the label is updated every second and everything works well.
Then after a random time interval, which changes from execution to execution, the label stops being updated. I've put a breakpoint in the timer update code and it no longer gets triggered.
I've also put log statements in all the places where I would normally stop the timer, but none of those places are called.
What could be the problem?
EDIT: Here is the sample of the code
ActionListener actionListener = new ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent arg0) {
secondsRemaining--;
System.out.println("Seconds remaining:" + secondsRemaining);
//update the progressbar
double initialLength = currentSettings.getLength()*60;
double progress = (initialLength - secondsRemaining)/initialLength ;
progressBarTime.setProgress(progress);
//update the progress label
progressPercentage.setText(((int)(progress * 100)) + "%");
if (secondsRemaining >= 0) {
updateTimeRemaining(secondsToString(secondsRemaining));
} else {
System.out.println(">0 seconds TIMER STOPPED with the number of seconds = " + secondsRemaining);
treatmentTimer.stop();
// set the status to Finished
currentState = State.FINISHED;
}
}
}
And the timer initialization:
tTimer = new Timer(1000, actionListener);
tTimer.start();
What's strange is that the program works fine on a PC with JRE 7u7 installed, i.e. the timer updates the label successfully, but I've tried on two PCs with 7u10 and this timer stopping issue happens on both.

Exception might be thrown, use try catch or Use UncaughtExceptionHandler to trace.

So I think I solved the problem. The garbage collector was removing the timer instance, for some unknown reason. I put a
System.out.println("message");
Inside the actionlistener of the timer, so that it would not be garbage collected.

Related

Timer (Clock) in JavaFX 1.8

I am trying to add a timer in my screen recording application being developed using JavaFX (IDE: NetBeans) and OS (Win 10). I am adding a timer now, which is to be used as:
User clicks on start recording, A Label on my application should start showing the time as 00:00:00, I have the time in seconds which is being printed on console even but when I use it to be displayed on application it just shows 0 Seconds. I am attaching code too. Kindly help. How can I refresh app interface or run timer when I have the value, It is just not updating.
Here you can see my app doesn't start timer; however, is being printed on consoleApp's Snap Console Snap
private void scheduleTimerTasks() {
isRecording = true;
int delay =50;
long period = 500;
//1000 / 48;
RecordTimer.reset();
timerRecord = new Timer("Thread TimerRecord");
timerCount = new Timer("Thread TimerCount");
recorderTask = new ScreenRecorderTask(encoder, rectangle);
countTask = new TimerCountTask(Timer);
timerRecord.scheduleAtFixedRate(recorderTask, delay, period);
timerCount.scheduleAtFixedRate(countTask, delay, period);
Timer.setText(""+countTask.timeInSec+" s"); //Setting Label Text
System.out.println(countTask.timeInSec);
recordStateLabel.setText("recorder Started...");
}
You could use AnimationTimer from javafx.animation.animationTimer, the handle' method of this timer will be called every frame.

Using Timer to repaint in the fixed time then continuing calculation

Currently, I am making a Java program for graph visualization Prim's algorithm in finding minimum spanning tree.
Here is the image of my program's output
while(condition){
//Find the min vertex and min edges
Vertex vertex = findMinVertex();
Edge[] edges = findMinEdges();
//Then, for each vertex and edges I found, I will change the color of
//them and pause the program for 3 seconds, so user can see how
//algorithm works.
repaintAndPause(3000);
}
.
.
private void repaintAndPause(int time){
long start = System.currentTimeMillis();
long end = start + speed;
//Here is the timer for repainting.
Timer timer = new Timer(speed, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e){
GraphPanel.this.repaint();
}
});
timer.setRepeats(false);
timer.setDelay(0);
timer.start();
//And here is for pausing the program, a while loop without any commands.
while(System.currentTimeMillis() < end){}
}
However, I don't know why but the program doesn't work. Yes, there are the pauses of program but, all the edges and vertices are just changed the color at the end of program. They aren't changed every 3 seconds.
Could someone please tell me where I did wrong?
Thank you and hope you have a nice day!
Could someone please tell me where I did wrong?
Yes. You are putting a busy-loop in the Event Dispatching Thread.
while(System.currentTimeMillis() < end){}
You code reads:
do some calculation (busy)
when done, post a "repaint" message, to redraw the panel when not busy
continue being very busy doing nothing for 3 seconds
continue being busy by repeating steps 1 through 4.
The Event Dispatching Thread never finishes processing the first "event" until the end of the algorithm, after the while (condition) loop finally finishes.
You want:
Timer timer = new Timer(speed, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
/* Code to perform one step of Prim's algorithm here */
/* Change edge/vertex colour for step */
/* Call timer.stop(), once done */
GraphPanel.this.repaint();
}
});
timer.setRepeats(true);
timer.setDelay(3000);
timer.start();
On every tick of the timer (once every 3 seconds), one step of the algorithm is performed.
Note this means that each step of the algorithm must run with any partial results stored into class members, so the the next step will be able to retrieve all the information it needs to continue. Stack variables can only be used inside one step; they cannot be used to hold inter-step values.
You could rework the algorithm to use a SwingWorker to run the calculation in its own background thread, and publish intermediate results when computed. The EDT could then repaint as intermediate results are produced. With a Thread#sleep() call, this background thread could delay production of intermediate results to once per 3 seconds.
Alternately, you could run the algorithm, and store multiple copies of the output, once for each 'step'. Then your Panel timer could simply show the output of the next step.

How can I create a JUnit test that runs until a certain condition is met?

I have a simple java snake game. I'm trying to test what happens when I press a lot of keys in a short interval.
Snake is my frame and Board is the JPanel where everything happens.
boolean stop=false;
Snake snake= new Snake();
KeyEvent key_right = new KeyEvent(snake.board, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_RIGHT,'Z');
KeyEvent key_up = new KeyEvent(snake.board, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_UP,'Z');
KeyEvent key_down = new KeyEvent(snake.board, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_DOWN,'Z');
KeyEvent key_left = new KeyEvent(snake.board, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_LEFT,'Z');
KeyEvent vect[]={key_right,key_down,key_left,key_up};
int i=0,nr=0;
Timer timer = new Timer();
#Test
public void StresTaste() {
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if(!stop){
snake.board.getKeyListeners()[0].keyPressed(vect[i]);
System.out.println(nr);
i++;nr++;
if(i==3) i=0;
if(nr==200){stop=true;}
}
}
}, 1, 1);
}
I create an array of keyEvents and loop over it using the i variable. I pass them to the keyListener of the JPanel.
Everything works fine.. but it stops at something over one hundred...not the same value everytime.
Please help. Is this even possible to implement?
A timer is a daemon thread, i.e. when one of them is around, that won't stop the VM from stopping.
So what happens is that you start the tests, create the timer and then stop the VM (because there are no more JUnit tests). The timer gets to process a couple of times but eventually, the termination of the VM kills it.
The solution is to add a lock to your code:
final Object lock = new Object();
timer.scheduleAtFixedRate(new TimerTask() { ...
if(stop) { synchronized(lock) { lock.notify(); }} // let test know that we're done
});
synchronized(lock) { lock.wait(); } // wait for timer to finish
That said, it's probably futile to test how many keypresses your code can process per second. The hardware, OS and human hands put a limit of about 215 words per minute.
Most often, the code doesn't care how fast it's being called but how often (memory leaks). So you can probably change your code to just loop over the key sequence and simply call snake.board.getKeyListeners() without a background thread (which, incidentally, will also give you hundreds of thousands of invocations per second).

Making moving motion with labels using threads in java

I'm having a problem I'm making a pool game and I need the ballos to react when I simulate a hit, the program works like this, you click the direction and power to hit the ball and the click go, the go button is in the GUI class where my labels are created, the button calls a method from my main class that recieves the parameter and then with a while in it, changes the X and Y of the ball till the power is reduced to 0 and then stops, the code is working, but the ball moves until the while stops. So the while works and when the power int is 0 the while goes out and then the new X,Y are painted.
This is the funcion that the button calls, the button sends all the parameters
public void golpe(int pbola, int pvelocidad, String pdireccion, JLabel[] listalabels) throws InterruptedException{
listabolas[pbola].setVelocidad(pvelocidad);
listabolas[pbola].setDireccion(pdireccion);
while (listabolas[pbola].getVelocidad() > 0) {
moverBola(pbola, listalabels);
//System.out.println(listabolas[pbola].getPosX());
//System.out.println(listabolas[pbola].getPosY());
Thread.sleep(500);
//This line is supposed to change the X and Y of the object over and over
//but only does it till the end
listalabels[pbola].setLocation(listabolas[pbola].getPosX(), listabolas[pbola].getPosY());
}
}
Here is the function moverbola(), only copied one "if" so that the code doesn't look to big
private void moverBola(int pbola, JLabel[] listalabels) {
if (listabolas[pbola].getDireccion().equals("SE")) {
int pposX = listabolas[pbola].getPosX();
listabolas[pbola].setPosX(pposX + 1);
int pposY = listabolas[pbola].getPosY();
listabolas[pbola].setPosY(pposY + 1);
}
Swing is a single threaded framework. That is, all interactions with UI are expected to occur from within a single thread, known as the Event Dispatching Thread.
Any action that blocks this thread, will prevent the EDT from updating the screen or processing any new events.
Your while-loop is blocking the EDT, preventing it from painting any updates until after the while-loop is completed.
Take a look at Concurrency in Swing for more details.
There are a number of approaches you could take...
You could use a Thread, but this causes problems as you need to ensure that any changes you make to the UI are re-synced back to the EDT and this can become messy...
For example
You could use a javax.swing.Timer that ticks at a regular interval and you would update any internal parameters from within it's assigned ActionListener. Because the tick events occur within the EDT, it is save to update the screen from within it.
For example
You could use a SwingWorker to run the task in the background. It has methods for re-syncing updates back to the EDT, but might be a little over kill for your purposes...
Updated with a possible Timer example
Caveat- It is very hard to produce a reasonable example with only a code snippet, but, something like this might work
public void golpe(final int pbola, int pvelocidad, String pdireccion, final JLabel[] listalabels) throws InterruptedException{
listabolas[pbola].setVelocidad(pvelocidad);
listabolas[pbola].setDireccion(pdireccion);
Timer timer = new Timer(40, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (listabolas[pbola].getVelocidad() == 0) {
((Timer)evt.getSource()).stop();
} else {
moverBola(pbola, listalabels);
}
}
});
timer.setRepeats(true);
timer.start();
}

How to change a JLabel's text in a loop without switching so fast the user can't see it?

I want to create a simple clock using Java. The code is so simple that I will give an example:
for(int i=0;i<=60;i++)
jLabel11.setText( Integer.toString(i) );
The problem is while I'm running my program the result didn't show each update in sequence.
It show only the 60 digit immediately, without showing the change from 1 to 2 to 3 ...
How can i fix this problem?
The problem is that changes to the UI should run on the event dispatch thread, but blocking this loop (and blocking the UI) will stop the screen from repainting. Instead, use a timer to perform regular updates, e.g.
Timer timer = new Timer();
ActionListener updater = new ActionListener()
{
int count;
public void actionPerformed(ActionEvent event) {
jLabel11.setText( Integer.toString(count++) );
if (count==60)
timer.stop();
}
}
timer.setDelay(100);
timer.addActionListener(updater);
timer.start();
See the Sun Tutorial - How to use Swing Timers.

Categories