how can I stop a thread from sleeping?

M

mimisam

hi,

I have a program(like the following codes) which needs to sleep for 20
seconds. How can i make it stop sleeping if i want it to be alive
before 20 seconds is up?(e.g. when a cancel button is clicked).
Any help will be appreciated !!
Thanks.

public static void a()
{
delay(20000);
}

public static void delay(int d)
{
try {
Thread.sleep(d);
} catch (InterruptedException e) {}
}
 
C

Christophe Vanfleteren

mimisam said:
hi,

I have a program(like the following codes) which needs to sleep for 20
seconds. How can i make it stop sleeping if i want it to be alive
before 20 seconds is up?(e.g. when a cancel button is clicked).
Any help will be appreciated !!
Thanks.

public static void a()
{
delay(20000);
}

public static void delay(int d)
{
try {
Thread.sleep(d);
} catch (InterruptedException e) {}
}

You might want to check the answer you got in c.l.j.help instead of posting
here again 3 minutes later.
 
A

Andrew Thompson

Christophe Vanfleteren said:
mimisam wrote: ....
You might want to check the answer you got in c.l.j.help instead of posting
here again 3 minutes later.

This is referred to as multi-posting.
You might benefit from checking Jon Skeet's
article on 'How to ask questions on Newsgroups'
http://jinx.swiki.net/79

Lots of great tips ..including some I should pay more
attention to myself!
 
?

=?ISO-8859-1?Q?=D2scar_P=E9rez_del_Campo?=

Hi,
the method Thread.interrupt() does exactly what you want. In your cancel
button you should call "threadToInterrupt.interrupt()" over the thread
object you want to resume.
Remember to catch the InterruptedException on your "delay" method (if
needed).

Òscar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top