A sentence out of my anticipation!

J

Jack Dowson

Hello Everybody:
Here is a demo to test java's multithread:
class MyThread extends Thread{
public void run(){
while(true){
System.out.println(getName() + " is running!");
try{
sleep(1000);
}catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
}
}
class InterruptThreadDemo{
public static void main(String[] args) throws InterruptedException{
MyThread m = new MyThread();
System.out.println("Starting thread...");
m.start();
Thread.sleep(2000);
System.out.println("Interrupt thread...");
m.interrupt();
Thread.sleep(2000);
System.out.println("Stopping application...");
}
}


The result is:
Starting thread...
Thread-0 is running!
Thread-0 is running!
Interrupt thread...
Thread-0 is running!
sleep interrupted
Thread-0 is running!
Thread-0 is running!
Stopping application...
Thread-0 is running!
Thread-0 is running!
Thread-0 is running!
Thread-0 is running!
Thread-0 is running!
.......


The outcome is bit out of my anticipation:
What happened to create the sentence "sleep interrupted!"

Any help will be greatly appreciated!
 
G

Gordon Beaton

The outcome is bit out of my anticipation:
What happened to create the sentence "sleep interrupted!"

You called m.interrupt() from main(), which resulted in an exception
in thread "m", currently sleeping. The exception contained the message
which you then printed in the catch block in run().

/gordon

--
 
J

Jack Dowson

Gordon Beaton дµÀ:
You called m.interrupt() from main(), which resulted in an exception
in thread "m", currently sleeping. The exception contained the message
which you then printed in the catch block in run().

/gordon
So when we interrupt a sleep thread it will become runnable!
Right?

Thank you!
 
J

Jack Dowson

Gordon Beaton дµÀ:
You called m.interrupt() from main(), which resulted in an exception
in thread "m", currently sleeping. The exception contained the message
which you then printed in the catch block in run().

/gordon
So that means if we interrupt a sleep thread then it will become
runnable,right?

Thank you!
 
P

Patricia Shanahan

Jack said:
Gordon Beaton дµÀ:
So that means if we interrupt a sleep thread then it will become
runnable,right?

Yes, an interrupt ends a sleep etc.

If you want to be sure to sleep for a specified time regardless of
interrupt, you need to keep track of when the sleep is due to end, and
put the sleep in a while loop.

However, it is more usual to send an interrupt to a thread when it
should stop what it is currently doing and do something else.

Patricia
 
J

Jack Dowson

Patricia Shanahan 写é“:
Yes, an interrupt ends a sleep etc.

If you want to be sure to sleep for a specified time regardless of
interrupt, you need to keep track of when the sleep is due to end, and
put the sleep in a while loop.

However, it is more usual to send an interrupt to a thread when it
should stop what it is currently doing and do something else.

Patricia
Thank you so much!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top