Rob
SCJP 1.4
Originally posted by Rob Ross:
You have created two objects. Then you start two threads , one for each object.
However, each thread is using the System.out PrintWriter object, and that is where you have thread contention.
Try synchronizing on System.out instead of your run method and see what happens.
Rob
Rob
SCJP 1.4
Rob
SCJP 1.4
Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Rob
SCJP 1.4
Originally posted by sylvia weller:
Thanks for your example Rob. I believe you are
right but something still baffles me.
Wouldn't it be correct that becuase this room is
'synchronized' only one person can be in it any
time, and person2 must wait until person1 leaves
before coming in to use the marker? So there'd
be no grabbing possible, and no coffee breaks.
Each person has enter the room, draw a square,
draw a triangle,and leave, in that order.
How could a person leave the room before both
shapes are completely drawn?
Sorry to beat this to death, but what am I not
understanding? Thank you.
Sylvia
Originally posted by Rob Ross:
By synchronizing on the critical resource (System.out) you ensure that only one thread at a time can write to it completely before another thread starts writing to it as well.
Rob
Originally posted by mark stone:
sylvia: you are correct where the confusion is.
BUT in the cert exam what should we answer ?
plain situation:
run is synchronized and it prints out say 5 numbers sequentially.
and say three threads have to run this run method.
what do the sun guys expect us to answer ?
PS: actually we have some "astute" guys here who could figure out the real reason.
Rob
SCJP 1.4
Rob
SCJP 1.4
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|