• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
raj yagnik
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know which Thread will execute first main Thread or spaw Thread??
I am giving u the code
public class twothread extends Thread {
public void run(){
for(int i=0; i<10; i++) {
System.out.println("New Thread");
}
}
public static void main() {
twothread t=new twothread();
t.start();

for(int i=0; i<10; i++) {
System.out.println("main Thread");
}
}
}
which will get printed first?
main thread or New Thread and why?
 
Sharada Kumaran
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't say for sure the order in which it will be printed.
t.start() will cause the thread to go into Ready to run state. Here it will wait before going into Running state. The thread scheduler will decide when the thread goes into Running.
Only on entering the Running state, will run() be executed.
Meanwhile, the for loop may/may not get executed. Or during the loop iteration, the run() might get executed.
You can't tell for sure.
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there's any way to tell wich will be printred first, or if it will all be printed in order. It has to do with what ever the processor decides is appropriate.
/Mike
 
raj yagnik
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know which Thread will execute first main Thread or spaw Thread??
I am giving u the code
public class twothread extends Thread {
public void run(){
for(int i=0; i<10; i++) {
System.out.println("New Thread");
}
}
public static void main() {
twothread t=new twothread();
t.start();
for(int i=0; i<10; i++) {
System.out.println("main Thread");
}
}
}
which will get executed first?
main thread or New Thread and why?

 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't you read the replies?
Once you create the other thread, your program now contains two treads. The way your program is written, there is no way of telling which of the two (the main thread, and the "new thread") will be runned first. This is up to the thread manager, which is sort of part of the JVM (or rather the processor architecture). It isn't even sure that it will run one of the threads to it's end before continueing with the next.
Did that answer your question?
/mike
 
raj yagnik
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanxs for clarification
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mikael,
It's depends on the type of scheduler whether the thread will run to it's end before continuing with other thread.If the scheduler is time slice then u certainly cannot be sure of the completion of thread in one slice of time.
What will happen if two threads are of same priority in the ready state.?
Acoording to a book introduction to software the scheduler will adopt a first come first served approach(FiFo).The thread which it runs first will run till it ends completly.But the java on a preemptive system does not do that it run's both the threads simultaneously.
Any comment's on that are welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic