Who can tell me how this program runs?

J

Jack Dowson

Hello Everybody:

Here is a program written to learn the multithread character of java!
class NewThread4 extends Thread{
NewThread4(String name){
super(name);
}
public void run(){
for(int count = 10,row = 0; row<10 ; row++){
for(int i = 0; i< count ; i++)
System.out.print('*');
System.out.println();
}
System.out.println(currentThread().getName() + " is over");
}
}
class ThreadDemo4{
public static void main(String[] args){
NewThread4 thd = new NewThread4("new Thread");
thd.start();
for(int i =0 ; i<10; i++){
System.out.println(Thread.currentThread().getName()+ " is running");
}
System.out.println(Thread.currentThread().getName() + " is over!");
}
}

The output is out of my anticipation!
I don't know how it works?


Any reply will be greatly appreciated!
 
D

Daniel Pitts

Hello Everybody:

Here is a program written to learn the multithread character of java!
class NewThread4 extends Thread{
NewThread4(String name){
super(name);
}
public void run(){
for(int count = 10,row = 0; row<10 ; row++){
for(int i = 0; i< count ; i++)
System.out.print('*');
System.out.println();
}
System.out.println(currentThread().getName() + " is over");
}
}
class ThreadDemo4{
public static void main(String[] args){
NewThread4 thd = new NewThread4("new Thread");
thd.start();
for(int i =0 ; i<10; i++){
System.out.println(Thread.currentThread().getName()+ " is running");
}
System.out.println(Thread.currentThread().getName() + " is over!");
}
}

The output is out of my anticipation!
I don't know how it works?

Any reply will be greatly appreciated!

Well, I'm not sure what you're seeing, but the point is that once you
call thd.start(), a new thread is spawned, and the run method of your
NewThread4 is executed in that other thread.

What this means is that the code in your run() method can run at the
same time as the code in your main method.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top