Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > creating a new thread extending thread class

Reply
Thread Tools

creating a new thread extending thread class

 
 
Aditi
Guest
Posts: n/a
 
      06-24-2005
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???

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      06-24-2005
Aditi <(E-Mail Removed)> wrote:
> 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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Aditi
Guest
Posts: n/a
 
      06-24-2005

Thanks. :-p

 
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
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Class.new and extending it... Thomas E Enebo Ruby 2 09-11-2005 03:55 PM
Extending class to be a base class *properly* Matthias Kaeppler C++ 1 04-22-2005 06:05 AM
maps and class types: extending a class factory Simon Elliott C++ 0 01-11-2005 01:02 PM
Extending a class - initialising new members. Fred C++ 6 10-26-2004 12:19 PM



Advertisments