Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Thread

 
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?
 
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.
 
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
 
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.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic