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