This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Threadss

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey ppl
i waneted to confirm this

main ALWAYS!!! completes before Threads initiated by start() executing assuming the following code.
public static void main(String s[])
{
//code crating thread
t1.start();
t2.start();
System.out.prinltn("hello");
}
will ALWAYS print hello before executing thread right???
regards Denish
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Denish,
That is a thought provoking question. The way I understand your question is the main would finish before the threads were started. I would think the run() method is initiated in the order they appear from: t1.start() t2.start() then the main method would call the System.out.println("hello"). In this case the main method would still finish executing before the threads were done. Correct me if I'm wrong.
Percy
 
Enthuware Software Support
Posts: 4599
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite right. Once you start 2 thread in main, there are 3 threads to be considered: the 2 that you started and the main() thread itself. Now, it's up to the JVM/OS thread scheduler which thread runs when and for how long. It is entirely possible that t1 runs and finishes, then t2 runs and finishes and then main finishes. Or any other combinaltion is also possible. You just cant say.

-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Try out the world's only WebCompiler!
www.jdiscuss.com
 
Percy Densmore
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Therefore synchronization would come in handy here, otherwise the order of execution is unpredictable. Thanks for the clarification.
Percy
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot Pual but please help me further.

I have "KAM" and "Java 2 complete by RHE". Both have given explicit examples regarding that main ALWAYS complets first (KAM page 275-276). Please guide me as to what should i answer in SCJP if asked a question concerinig this.
I shall be very obliged of any help provided
Regards Denish
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class popo implements Runnable
{
public static void main(String[]args)
{
popo p=new popo();
Thread t=new Thread(p);
Thread t1=new Thread(p);
t.start();
t1.start();
System.out.println("hello how are u");
System.out.println("hello how are u");
System.out.println("hello how are u");
System.out.println("hello how are u");
System.out.println("hello how are u");
System.out.println("hello how are u");
System.out.println("hello how are u");
System.out.println("hello how are u");
System.out.println("hello how are u");
}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println(i);
}
}
}
}
Hi
Have a look at the above given code, u will see that main thread has ended last.In case if u get a question like this in an exam then your answer should be that u cannot tell which thread will run first and which one will end last.It is totally up to the scheduler to decide which thread will run first and which will not.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class popo implements Runnable
{
public static void main(String[]args)
{
popo p=new popo();
Thread t=new Thread(p);
Thread t1=new Thread(p);
t.start();
t1.start();
try
{
t.join();
t1.join();
}
catch(InterruptedException e)
{
}
System.out.println("hello how are u");
}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println(i);
}
}
}
hi denish,
In the above given code i have made use of the join method which make's the parent thread to wait for the child thread to complete.Now, in the above code main will alway's end in the last.Run it again and again and u will see that "hello how are u alway's come's last in the output.
I hope it help's
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot nitin R u using Windows?? If yes (i am )may windows is using time slicing approach that is relinqueshes control from the main and giving chance to other Threads as well.
It was just a thought Other wise i completly now agree with u and paul
regards Densih
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i am using windows.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic