Dany Mariana wrote:I understand that synchronized means that 2 threads can't access in the same time a method. This means they can go out a method before completed and reenter later to complete. Am I wrong?
For every winner, there are dozens of losers. Chances are you're one of them.
Dany Mariana wrote:Hello,
After completing the test from Thread chapter, one question from it is not very clear to me.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper Young wrote:
Dany Mariana wrote:Hello,
After completing the test from Thread chapter, one question from it is not very clear to me.
From which book is this, the SCJP Study Guide by Kathy Sierra and Bert Bates (also known as K&B)? Please mention the source when you copy a question, not everybody has the same book as you.
Horia Constantin wrote:
Dany Mariana wrote:I understand that synchronized means that 2 threads can't access in the same time a method. This means they can go out a method before completed and reenter later to complete. Am I wrong?
If you have a synchronized method and one thread enters it, no other threads can enter it (or alter the objects/classes that it involves) until the thread exists the method.
In your case, for synchronized void chat(long id) the first thread that enters the method finds the flag=0 (keep in mind that once it enters the method it will run until the for loop ends) and then the if is true so flag gets set with the id value so the if (flag==id) will return true every time, therefore the output should be:
yo yo dude dude (this being the only possible alternative).
Cheers,
Horia
For every winner, there are dozens of losers. Chances are you're one of them.
Horia Constantin wrote:Give me some time to make some tests as I seem to have been misled by your example and I think the explanation is different from the ones I gave you
The new code you wrote is quite different from what you had in the beginning.
In the first version (Dudes) you had a static long flag. When entering the synchronized method, the thread got the monitor lock for the class (aka because you have static the thread gets the lock for the class). And both threads in the example use the same class lock, that's why one of the threads must wait for the other one to finish.
Am I clear until now?
On the other hand, in your second example (TestThreadSyn) you designed a class with threads that get objects' lock (they are synchronized on the actual Threads objects, which are different; therefore the output is unpredictable).
Was I explicit enough?
Cheers,
Horia
Dany Mariana wrote:Sorry but I think I don't understand. In the first example, the one from the book, the look is for object not for class. It is a method syncronized and the syncronized object is this no a static object of the class.
Horia Constantin wrote:Give me some time to make some tests as I seem to have been misled by your example and I think the explanation is different from the ones I gave you
The new code you wrote is quite different from what you had in the beginning.
In the first version (Dudes) you had a static long flag. When entering the synchronized method, the thread got the monitor lock for the class (aka because you have static the thread gets the lock for the class). And both threads in the example use the same class lock, that's why one of the threads must wait for the other one to finish.
Am I clear until now?
On the other hand, in your second example (TestThreadSyn) you designed a class with threads that get objects' lock (they are synchronized on the actual Threads objects, which are different; therefore the output is unpredictable).
Was I explicit enough?
Cheers,
Horia
For every winner, there are dozens of losers. Chances are you're one of them.
Horia Constantin wrote:I'm sorry for misinforming you in the beginning (apparently I was also misinformed). But it's clear to me now.
According to my revised explanation from above, this time you are using your threads with different objects (ch and new C()).
new Thread(ch).start(); gets the object lock for the ch object
new Thread(new C()).start(); gets the object lock for a new C object.
Soooo, different locks, unpredictable output.
If you were to write:
the output would be as you expected: yo dude yo dude yo dude yo dude
Hope I'm clear this time.
Cheers,
Horia
P.S.: Thanks for the opportunity you gave me to study the threads deeper.
The new code you wrote is quite different from what you had in the beginning.
In the first version (Dudes) you had a static long flag. When entering the synchronized method, the thread got the monitor lock for the class (aka because you have static the thread gets the lock for the class). And both threads in the example use the same class lock, that's why one of the threads must wait for the other one to finish.
Am I clear until now?
On the other hand, in your second example (TestThreadSyn) you designed a class with threads that get objects' lock (they are synchronized on the actual Threads objects, which are different; therefore the output is unpredictable).
Was I explicit enough?
Cheers,
Horia
Don't get me started about those stupid light bulbs. |