Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to call Thread.sleep() or Thread.yield() without enclosing try/catch block ?

4,167 views
Skip to first unread message

lonelyplanet999

unread,
Feb 1, 2004, 10:40:15 AM2/1/04
to
Hi,

I read a java programmming book which talks about Thread.sleep() in
following way.

"the sleep() method can throw a checked InterruptedException, so
your're forced to acknowledge the exception with a handle or declare.
Typically, you just wrap each call to sleep in a try/catch, ...

I tried not using the try/catch block but declares the class
implementing Runnable to throw the checked exception. This certainly
couldn't pass compiler as run() is final and couldn't be overriden. I
tried changing the implementation by extending Thread class alone but
still failed as compiler said run() method of java.lang.Thread
couldn't be overriden to throw InterruptedException.

public class Test7 {
public static void main (String [] args) {
SleepRunnable r = new SleepRunnable();
Thread t = new Thread(r);
t.start();
}
}

class SleepRunnable implements Runnable {
public void run() throws InterruptedException {
Thread.sleep(1000);
}
}

I would like to ask if I use Thread.sleep() or Thread.yield(), is
try/catch block not avoidable i.e. no other ways to tell compiler to
throw out exception should it occur ?

:)

Christophe Vanfleteren

unread,
Feb 1, 2004, 11:14:01 AM2/1/04
to
lonelyplanet999 wrote:

No you can't do those things you tried. You can't just go about changing
method signatures and expect things to work.

When you don't care that your thread is interrupted while sleeping (many
times there is no reason to care), just catch the exception and ignore it.

class SleepRunnable implements Runnable {
public void run() throws InterruptedException {

try{
Thread.sleep(1000);
}catch(InterruptedException e) {
//IGNORED
e.printStackTrace();
}
}
}

--
Kind regards,
Christophe Vanfleteren

Christian Kaufhold

unread,
Feb 2, 2004, 12:24:28 PM2/2/04
to
Christophe Vanfleteren <c.v4nf...@pandora.be> wrote:

>
> class SleepRunnable implements Runnable {
> public void run() throws InterruptedException {
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Remove.


> try{
> Thread.sleep(1000);
> }catch(InterruptedException e) {

Thread.currentThread().interrupt();


Christian

lonelyplanet999

unread,
Feb 3, 2004, 3:10:42 AM2/3/04
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0T10olsg...@uni.chka.de>...

But the point is I want to make the Thread to sleep for 1 second, not
to force it to interrupt explicitly. However, if I don't enclose
Thread.sleep with try/catch block, compilation will fail. Yet, I want
to ask if try/catch block is the only way to make Thread.sleep
compilable! :)

>
>
>
>
> Christian

lonelyplanet999

unread,
Feb 3, 2004, 3:11:23 AM2/3/04
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0T10olsg...@uni.chka.de>...

But the point is I want to make the Thread to sleep for 1 second, not

lonelyplanet999

unread,
Feb 3, 2004, 3:11:22 AM2/3/04
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0T10olsg...@uni.chka.de>...

But the point is I want to make the Thread to sleep for 1 second, not

lonelyplanet999

unread,
Feb 3, 2004, 3:11:20 AM2/3/04
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0T10olsg...@uni.chka.de>...

But the point is I want to make the Thread to sleep for 1 second, not

lonelyplanet999

unread,
Feb 3, 2004, 3:13:41 AM2/3/04
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0T10olsg...@uni.chka.de>...

That's change to original program logic. My code doesn't mean to
explicitly calling interruption. It just wants to make the thread
sleep. Calling Thread.sleep without try/catch block makes compilation
fails. I just want to know if using enclosing try/catch block is the
only way for using Thread.sleep() ? :)

>
>
>
>
> Christian

lonelyplanet999

unread,
Feb 3, 2004, 3:15:46 AM2/3/04
to
Christophe Vanfleteren <c.v4nf...@pandora.be> wrote in message news:<dB9Tb.2261$t63.2...@phobos.telenet-ops.be>...

>
> No you can't do those things you tried. You can't just go about changing
> method signatures and expect things to work.

I know I couldn't change run()'s signature but I would like to ask if
using enclosing try/catch block is the only way to call Thread.sleep()
or Thread.yield() ? :)

Christophe Vanfleteren

unread,
Feb 3, 2004, 4:11:38 AM2/3/04
to
lonelyplanet999 wrote:

> Christophe Vanfleteren <c.v4nf...@pandora.be> wrote in message
> news:<dB9Tb.2261$t63.2...@phobos.telenet-ops.be>...
>
>>
>> No you can't do those things you tried. You can't just go about changing
>> method signatures and expect things to work.
>
> I know I couldn't change run()'s signature but I would like to ask if
> using enclosing try/catch block is the only way to call Thread.sleep()
> or Thread.yield() ? :)

Yes, using a try/catch block is the only way. Now that can't really be such
a problem, can it?

Michiel Konstapel

unread,
Feb 3, 2004, 5:40:54 PM2/3/04
to
On 3 Feb 2004 00:11:20 -0800, lonelyplanet999 <lonelyp...@my-deja.com>
wrote:

Since you can't throw checked exceptions from run(), yes, you'll need a
try/catch to make it compile. There's basically two ways to go about this.
Either assume you will not be interrupted (and accept that you'll sleep too
short if you are):

try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}

Or, if you're sure you want to sleep for at least a second, even if you are
interrupted, just keep trying:

long sleepUntil = System.currentTimeMillis() + 1000;
do {
try {
Thread.sleep(sleepUntil - System.currentTimeMillis());
} catch (InterruptedException ignored) {}
}
while (System.currentTimeMillis() < sleepUntil);

HTH,
Michiel

0 new messages