File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread doubt ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread doubt ??" Watch "Thread doubt ??" New topic
Author

Thread doubt ??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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


Thanks & Regards<br /> <br />-Srikanth
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Jul 28, 2005
Posts: 252
But steve here iam calling mt1.run() method explictly ...
will that have no impact ..

thanks

srikanth
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Dec 20, 2001
Posts: 3271


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.


SCJP Tipline, etc.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
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 ]

/** Code speaks louder than words */
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jul 28, 2005
Posts: 252
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread doubt ??