SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
ramesh maredu wrote:Output always will be 0246.
Using boolean parameter, the code ensures that run method will be executed before getResult completes.
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Isn't 2064 a possible result???
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
ramesh maredu wrote:
NO. order will be always same that is 0246
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
[List of FAQs] | [Android FAQ] | [Samuh Varta]
Monu Tripathi wrote:When getresult is blocked on say instance number 2, unless it is unblocked, the for loop will not move further to next instance.
All the thread operating on its own object, hence I believe synchronized is not needed
Second the threads might calculate in arbitrary order, but the printing is happening in a particular order in the snippet by the main thread
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Monu Tripathi wrote:
using this :
You can try this out with this :
I hope this makes sense.
ramesh maredu wrote: Yes. getResult is forced to wait to maintain order.
.Yes, but why the main thread will not move further with other threads? Lets assume the lock is held on second object, proceeding further doesn't require the same lock. Isn't it?
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Adeel Ansari wrote:
Your given snippet will hold the main thread forever.
[List of FAQs] | [Android FAQ] | [Samuh Varta]
ramesh maredu wrote:
Here order is maintained not because of locks, please note that threads are created on different objects.Synchronization key word is not doing anything there.
Assume that 0,2 are printed to console(means two for loops are executed twice), now assume that to print 4 JVM called getResult method.If run method on this object is not called yet, this particular thread will be waiting to get notified.
Mean time two options are possible
first is, JVM can call another execution of second for loop, if it does so another thread(which is having num value 2) will be waiting because first for loop is not completed yet with 3 rd iteration.
Second is JVM can continue with first for loop, if it does so it will execute run method on third object so it will call notify method so that particular object will be notified and output 4 will be printed.
No. See my revisions.
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Don't get me started about those stupid light bulbs. |