• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread doubt ??

 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • 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
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • 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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic