Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A sentence out of my anticipation!

Reply
Thread Tools

A sentence out of my anticipation!

 
 
Jack Dowson
Guest
Posts: n/a
 
      05-04-2007
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!
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      05-04-2007
On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
> 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

--
 
Reply With Quote
 
 
 
 
Jack Dowson
Guest
Posts: n/a
 
      05-04-2007
Gordon Beaton д:
> On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
>> 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
>

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

Thank you!
 
Reply With Quote
 
Jack Dowson
Guest
Posts: n/a
 
      05-04-2007
Gordon Beaton д:
> On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
>> 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
>

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

Thank you!
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-04-2007
Jack Dowson wrote:
> Gordon Beaton д:
>> On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
>>> 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
>>

> 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
 
Reply With Quote
 
Jack Dowson
Guest
Posts: n/a
 
      05-04-2007
Patricia Shanahan 写道:
> Jack Dowson wrote:
>> Gordon Beaton дµÀ:
>>> On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
>>>> 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
>>>

>> 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

Thank you so much!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I can't understand a sentence "Look out for implicit assignments inwhile conditionals" in 'perlsub' Raymundo Perl Misc 1 04-23-2011 11:22 PM
reverse the order of the words in a sentence Matt Java 11 08-16-2010 09:05 AM
Simple problem, understanding the case sentence Geo VHDL 6 02-12-2006 04:07 AM
Simple problem, understanding the case sentence Geo VHDL 0 02-08-2006 06:39 AM
process sentence in synthesis Jluis VHDL 3 05-05-2004 10:07 AM



Advertisments