creating a new thread extending thread class

A

Aditi

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

Chris Smith

Aditi said:
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
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top