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 / June 2005

Tip: Looking for answers? Try searching our database.

creating a new thread extending thread class

Thread view: 
Aditi - 24 Jun 2005 15:52 GMT
Consider the following program of creating a new thread extending
thread class.
//creating a new thread extending thread.
class NewThread extends Thread {

NewThread() {
    super("Demo Thread");
    System.out.println("child thread"+this);
    start();}
public void run() {
    try {
        for(int i=5;i<0;i--){
        System.out.println("child thread:"+i);
        Thread.sleep(500);}
}
    catch (InterruptedException e)
    {System.out.println("child interupted");
    }
    System.out.println("exiting child thread");
}
}
class test {
           public static void main(String args[]) {
        new NewThread();
        try {
            for(int i=5;i>0;i--) {
                System.out.println("main thread"+i);
                Thread.sleep(500);
            }
}            catch(InterruptedException e) {
                System.out.println("main interupted");
            }
            System.out.println("exiting main thread");
    }
}
the output which i am getting is
child threadThread[Demo Thread,5,main]
main thread5
exiting child thread
main thread4
main thread3
main thread2
main thread1
exiting main thread

The child thread is entered and exited quickly without being executed
.I guess output of programs with sleep method may vary based on
processor speed and task load. Is this a correct explanation of such
behaviour or is it something else???
Chris Smith - 24 Jun 2005 16:04 GMT
> Consider the following program of creating a new thread extending
> thread class.

First of all, please learn to format your code.  I am having a hard time
imagining how you could come up with something so unreadable as what you
posted.  Line wrapping by your news reader might be part of it, but
other pieces are just utterly odd.

Second, this has nothing to do with threads.  You for loop condition in
the child thread is inverted.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Aditi - 24 Jun 2005 16:29 GMT


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.