• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread doubt ??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi frnds ,


in the above code i have one doubt ..
here when mt1.start(); is executed then its not sure that the run method is called or not ...but after that we called mt1.run() method ..here we are forcing it to execute the run method...so can we say that here we can get the predicted output (i.e mt1 is printed 100 times followed by mt2 ..
and that was as given wrong in the answer..

thanks & regards
srikanth
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so can we say that here we can get the predicted output (i.e mt1 is printed 100 times followed by mt2 ..


No; due to the fact that you have called mt1.start() (which will call the run() method), the output will be unpredictable. Do not rely on "consistent" behavior from the thread scheduler.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But steve here iam calling mt1.run() method explictly ...
will that have no impact ..

thanks

srikanth
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But steve here iam calling mt1.run() method explictly ...
will that have no impact ..


The only thing you can say is that the value of myName in mt1 will be printed at least 100 times before the mt2 thread starts (it could easily be more). The rest is not predictable.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the given code, Line 1 creates a new thread of execution, as does Line 3. So, once all three lines have been executed, we have 3 threads of execution.

Line 1 creates a new thread of execution which will, during its lifespan, print out mt1 100 times. When that gets done, however, is impossible to determine.

Line 2 will print out mt1 100 times. That might happen entirely before the aforementioned thread does, entirely after, or the output might be mingled together - we have no way to know.

Line 3 will create another thread of execution that will print mt2 100 times. Again, this might happen before the output of the first thread we created, it might happen afterward, or the output might be mingled together.

As was said earlier, the only thing that we really know "for sure" is that mt1 will be printed at least 100 times before mt2 is printed at all. But that's the only thing we can say, for certain.
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
[QBreally know "for sure" is that mt1 will be printed at least 100 times before mt2 is printed at all.[/QB]



There are 3 threads mt1,mt2 and the main thread. how can we be sure that mt1 will be printed 100 times before mt2 is printed at all - Cannot we have swapping between mt1,mt2 and main thread for printing the numbers. ??
[ September 10, 2005: Message edited by: Shivani Chandna ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shivani Chandna:


There are 3 threads mt1,mt2 and the main thread. how can we be sure that mt1 will be printed 100 times before mt2 is printed at all - Cannot we have swapping between mt1,mt2 and main thread for printing the numbers. ??

[ September 10, 2005: Message edited by: Shivani Chandna ]



No, because the main thread must finish printing "mt1" 100 times

before it even starts the mt2 thread.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok understood that mt2 cannot start before main ends.
but its possible that mt1 is started (is under execution) but then main comes finishes with the for loop and starts mt2 thread.-->mt1 thread has not yet finished execution...So then we have execution of thread mt1 and mt2 left ???

Is it possible or am i imagining things...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After the main thread has finished printing the "mt1" string 100 times, it is then possible for thread mt2 to run. As Corey wrote, "mt1" must be printed at least 100 times before the first "mt2" gets printed. It is possible, that after thread mt2 gets started, the threads main or mt1 get blocked until mt2 finishes, but main at least will have finished its work.

An afterthought: You do realise that by the main thread executing object mt1's run() method it is printing the name in the mt1 object - "mt1"? So that there are two threads printing "mt1".
[ September 10, 2005: Message edited by: Barry Gaunt ]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks corey ..
so that means the mt1 is printed atleast 100 times bfore the mt2 is printed coz of the main thread printing mt1 as mt1.run which is invoked as the normal method.


thanks
srikanth
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic