Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

A sentence out of my anticipation!

Thread view: 
Jack Dowson - 04 May 2007 09:57 GMT
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!
Gordon Beaton - 04 May 2007 11:46 GMT
> 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

--
Jack Dowson - 04 May 2007 14:14 GMT
Gordon Beaton 写道:
>> The outcome is bit out of my anticipation:
>> What happened to create the sentence "sleep interrupted!"
[quoted text clipped - 4 lines]
>
> /gordon

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

Thank you!
Jack Dowson - 04 May 2007 14:17 GMT
Gordon Beaton 写道:
>> The outcome is bit out of my anticipation:
>> What happened to create the sentence "sleep interrupted!"
[quoted text clipped - 4 lines]
>
> /gordon

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

Thank you!
Patricia Shanahan - 04 May 2007 14:24 GMT
> Gordon Beaton дµÀ:
>>> The outcome is bit out of my anticipation:
[quoted text clipped - 7 lines]
> 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
Jack Dowson - 04 May 2007 14:28 GMT
Patricia Shanahan 写道:
>> Gordon Beaton дµÀ:
>>>> The outcome is bit out of my anticipation:
[quoted text clipped - 18 lines]
>
> Patricia
Thank you so much!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.