954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why do I get this error? (Exception in thread "Thread-4" - timeout value is negative)

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?

Acegikmo
Light Poster
26 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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;
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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. ;)

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

It seems to work, thanks! :)

Acegikmo
Light Poster
26 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You