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..
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.
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 ]
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
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...
Joined: Aug 03, 2002
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 ]
Joined: Jul 28, 2005
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com