By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

interrupted exception in sleep why?

P: n/a
hi

I am displaying a digital clock in an applet.I am using Thread.sleep
(1000) for
counting every second.This works fine some times but some times my
clock just slows down or give an interrupted exception .I am not
calling Thread.interrupt anywhere in my program. why is it happening?
how to fix the problem?

Anks
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I thought with applets they automatically
call stop() when they are scrolled off screen.
Also, it is a waste to call sleep(300), use
sleep(1000).

"-wiseguy" <no****@all.net> wrote in message news:Xn*************************@216.65.98.9...
an*****@indiatimes.com (Anks) wrote in
news:23**************************@posting.google.c om:
hi

I am displaying a digital clock in an applet.I am using Thread.sleep
(1000) for
counting every second.This works fine some times but some times my
clock just slows down or give an interrupted exception .I am not
calling Thread.interrupt anywhere in my program. why is it happening?
how to fix the problem?

Anks


There are threads, other than those that you explicitly start, that run
anytime a JAVA VM is created. You have to trap and recover from
InterruptedException exceptions whenever you use Thread.sleep().

Don't rely on the Thread.sleep(1000) as a timer for keeping track of time.
It only says that your thread will sleep for a "minimum" of 1000ms.

Instead, use a Date object and sleep for less than one second, maybe 300ms,
before waking up and checking if the clock should be updated.

Don't forget to "stop" the applet when it is not visible...and restart when
it becomes visible again.



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

Jul 17 '05 #2

P: n/a
Not necessarily. It depends on the implementation of the
browser/sandbox. Certain versions of Netscape will let the Applet run
for 30 seconds and if it does not automatically stop, it will kill the
applet in an ungraceful manner.

Also, if your applet is very CPU intensive it is much nicer to respond
to the stop method so that the users' browsing experience is more
responsive when they leave your page.

-Misk

Phil wrote:
I thought with applets they automatically
call stop() when they are scrolled off screen.
Also, it is a waste to call sleep(300), use
sleep(1000).

"-wiseguy" <no****@all.net> wrote in message news:Xn*************************@216.65.98.9...
an*****@indiatimes.com (Anks) wrote in
news:23**************************@posting.google .com:

hi

I am displaying a digital clock in an applet.I am using Thread.sleep
(1000) for
counting every second.This works fine some times but some times my
clock just slows down or give an interrupted exception .I am not
calling Thread.interrupt anywhere in my program. why is it happening?
how to fix the problem?

Anks


There are threads, other than those that you explicitly start, that run
anytime a JAVA VM is created. You have to trap and recover from
InterruptedException exceptions whenever you use Thread.sleep().

Don't rely on the Thread.sleep(1000) as a timer for keeping track of time.
It only says that your thread will sleep for a "minimum" of 1000ms.

Instead, use a Date object and sleep for less than one second, maybe 300ms,
before waking up and checking if the clock should be updated.

Don't forget to "stop" the applet when it is not visible...and restart when
it becomes visible again.

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =--


-


Jul 17 '05 #3

P: n/a
On 21 Aug 2003 00:28:41 -0700, an*****@indiatimes.com (Anks) two-finger
typed:
Hi
Thanks for your response but i still didn't got it completely.
You have asked to use date object but i did not find any such method
in date classes which ensures a sleep of specified time(1000ms).How
can I use date objects in place of sleep.
Thanks

Anks
He meant that you use Thread.sleep() with a lower number in combination
with checking the date/time of the system to see if you need to update the
visual representation.

I would use System.currentTimeMillis() for faster more accurate
comparisons between the previous update and the next.

e.g. if you first update was done on 123456789 milliseconds, and after
System.sleep(250), it shows 123457500 (711 ms later), you can sleep another
289 ms before updating again.
"-wiseguy" <no****@all.net> wrote in message news:<Xn*************************@216.65.98.9>...
an*****@indiatimes.com (Anks) wrote in
news:23**************************@posting.google.c om:
> hi
>
> I am displaying a digital clock in an applet.I am using Thread.sleep
> (1000) for
> counting every second.This works fine some times but some times my
> clock just slows down or give an interrupted exception .I am not
> calling Thread.interrupt anywhere in my program. why is it happening?
> how to fix the problem?
>
> Anks
>


There are threads, other than those that you explicitly start, that run
anytime a JAVA VM is created. You have to trap and recover from
InterruptedException exceptions whenever you use Thread.sleep().

Don't rely on the Thread.sleep(1000) as a timer for keeping track of time.
It only says that your thread will sleep for a "minimum" of 1000ms.

Here is the bit you should have read carefully:
Instead, use a Date object and sleep for less than one second, maybe 300ms,
before waking up and checking if the clock should be updated. Don't forget to "stop" the applet when it is not visible...and restartwhen
it becomes visible again.
In other words: put a check in your thread loop for a boolean variable:

boolean keepRunning = true ;
public void run() {
keepRunning = true // when starting at first
while(keepRunning) { // loop until stop() is called
// update clock
repaint(); // assuming that paint() does all the work
// wait for about one second - or use more sophisticated timing
try { Thread.sleep(1000); } catch(InterruptedException ix) { break;}
}
}

public void stop() {
keepRunning = false ;
}



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Cheers.
Jul 17 '05 #4

P: n/a
"Phil" <ry***@ieee.org> wrote in news:5mU0b.157930$cF.56387@rwcrnsc53:
I thought with applets they automatically
call stop() when they are scrolled off screen.
Also, it is a waste to call sleep(300), use
sleep(1000).


My mistake in how I eplxained it. I mean't to say to override the necessary
methods so that applet and its associated thread don't take up CPU time when
the thing isn't visible.

re: 1000ms...a delay of 1 second can mean that your display might
occasionally miss the display of a second every once in a while.
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 17 '05 #5

P: n/a
Neomorph <ne******@nospam.demon.co.uk> wrote in
news:sj********************************@4ax.com:
On 21 Aug 2003 00:28:41 -0700, an*****@indiatimes.com (Anks) two-finger
typed:
Hi
Thanks for your response but i still didn't got it completely.
You have asked to use date object but i did not find any such method
in date classes which ensures a sleep of specified time(1000ms).How
can I use date objects in place of sleep.
Thanks

Anks


He meant that you use Thread.sleep() with a lower number in combination
with checking the date/time of the system to see if you need to update the
visual representation.

I would use System.currentTimeMillis() for faster more accurate
comparisons between the previous update and the next.

e.g. if you first update was done on 123456789 milliseconds, and after
System.sleep(250), it shows 123457500 (711 ms later), you can sleep another
289 ms before updating again.

Actually I would just be lazy and update the display with the current time
(acquired from a new Date object) every time that the applet thread becomes
runnable again. creating and displaying a text clock a few times each second
certainly isn't going to bog down the machine...at least it shouldn't. :^)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.