473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

interrupted exception in sleep why?

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
5 13954
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jakub Moscicki | last post by:
Hello, A small problem: I get a signal during a system call (from xmlrpclib -> httplib) and an exception "IOError: Interrupted system call" is raised (this is system dependant, on other machine...
0
by: Sylwia | last post by:
Hi! I have implemented a Python services. It behaves as a supervisor for log files. If the space used by log files is bigger than a given upper limit, then it starts to delete log files until...
2
by: Sylwia | last post by:
Hi! I need your help... I have the following problem. I've implemented the python Windows Service which behaves like a log supervisor. If the space used by log files is bigger than a given...
0
by: Nazgul | last post by:
Hi! Sorry if I posted it twice... I need your help... I have the following problem. I've implemented the python Windows Service which behaves like a log supervisor. If the space used by log...
1
by: José Joye | last post by:
When I unload my appdomain, I got the following exception: A blocking operation was interrupted by a call to WSACancelBlockingCall: ..... My appdomain uses remoting... From previous post, I...
1
by: Sagaert Johan | last post by:
Hi Ii have a simple server thread in an app that listens for connections, for some unclear reason an exception is thrown every now and then : 'A blocking operation was interrupted by a call to...
7
by: Marco | last post by:
Hello,every one, I meet a question: in my old script, I usually use os.popen2() to get info from standard unix(LinuX) program like ps,ifconfig... Now, I write a OO-based programme, I still use...
3
by: arjan321 | last post by:
Hello all, I have a strange problem with the .NET serialPort class. When I write some data to the serialport, not all the data is immediately send: every once in a while, only ~50 characters...
1
by: ashish | last post by:
Hi All, I wanted to know how to handle events like 'logoff' in the main thread so that any process which is being run by svcDoRun method of service does not get 'interrupted function call'...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.