After I've been running my program for a while, it freezes and gives me this error in the console:

Exception in thread "Thread-4" java.lang.IllegalArgumentException: timeout value is negative
at java.lang.Thread.sleep(Native Method)

It gives me a line to where the error occurs, which is the same line where it says "try":

while (true){

                if(nextRepaintIn > Calendar.getInstance().getTimeInMillis()){
                try {Thread.sleep(nextRepaintIn - Calendar.getInstance().getTimeInMillis());
                } catch (InterruptedException ex) {
                    // none;
                    }
                }
                nextRepaintIn = Calendar.getInstance().getTimeInMillis()+1000/maxFps;

Does anyone of you have any idea of what's wrong?

After I've been running my program for a while, it freezes and gives me this error in the console:

Exception in thread "Thread-4" java.lang.IllegalArgumentException: timeout value is negative
at java.lang.Thread.sleep(Native Method)

It gives me a line to where the error occurs, which is the same line where it says "try":

while (true){

                if(nextRepaintIn > Calendar.getInstance().getTimeInMillis()){
                try {Thread.sleep(nextRepaintIn - Calendar.getInstance().getTimeInMillis());
                } catch (InterruptedException ex) {
                    // none;
                    }
                }
                nextRepaintIn = Calendar.getInstance().getTimeInMillis()+1000/maxFps;

Does anyone of you have any idea of what's wrong?

Maybe Calendar.getInstance ().getTimeInMillis () changes during the for-loop? Perhaps change it to this?

while (true){
                int pause = nextRepaintIn - Calendar.getInstance ().getTimeInMillis ();
                if(pause > 0){
                try {Thread.sleep(pause);
                } catch (InterruptedException ex) {
                    // none;
                    }
                }
                nextRepaintIn = Calendar.getInstance().getTimeInMillis()+1000/maxFps;

The problem is that timeRepaintIn - Calendar.getInstance().getTimeInMillis() is a negative value. Which means that Calendar.getInstance().getTimeInMillis() is greater than timeRepaintIn. So you might want to rethink your logic on deciding how long to sleep for.

The problem is that timeRepaintIn - Calendar.getInstance().getTimeInMillis() is a negative value. Which means that Calendar.getInstance().getTimeInMillis() is greater than timeRepaintIn. So you might want to rethink your logic on deciding how long to sleep for.

He's testing for that here though, right?

if(nextRepaintIn > Calendar.getInstance().getTimeInMillis())

That's what leads me to believe that Calendar.getTimeInMillis () is different the first and second time it's called. The first time, the if-statement is true, so nextRepaintIn is greater than Calendar.getTimeInMillis () , but when it is called INSIDE the if-statement, nextRepaintIn is smaller than Calendar.getTimeInMillis () . That's why I recommended calling it only once.

Whoops. Good point. I'd give you more rep for that but apparently I can't since I gave you rep today.

Whoops. Good point. I'd give you more rep for that but apparently I can't since I gave you rep today.

I'll take a rain check. ;)

It seems to work, thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.