This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

raj yagnik
Greenhorn

Joined: Feb 14, 2001
Posts: 15
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

Joined: May 23, 2001
Posts: 29
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.


Sharon
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
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

Joined: Feb 14, 2001
Posts: 15
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

Joined: May 16, 2001
Posts: 158
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

Joined: Feb 14, 2001
Posts: 15
Thanxs for clarification
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
Another quesion about a gc worth discussion.(scjp07-Q130)
Thread
Doubt in the final !!
Doubt in threads !!
How can I get a run() method to return a string variable, when run() method is required to be void ?