Forums Register Login

Synchronized block of code

+Pie Number of slices to send: Send
I got this code from K & B:



What is the logic behind the synchronization on the letter object? Can you put just any object inside the synchronization parentheses just to satisfy the requirement of synchronizing a block of code? I'm trying to learn the synchronization concepts of threads and I'm looking for a clear cut explanation on this concept.
+Pie Number of slices to send: Send
Yes, you can practically use any object as the synchronization lock... but there is a reason why the stringbuffer was chosen. It is the same instance with all three threads, and synchronization was done to prevent the threads from stepping on each other.

So if you use any other object, you have to make sure that the three threads are using the same instance / object.

Henry
[ April 24, 2005: Message edited by: Henry Wong ]
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 810 times.
Similar Threads
Doubt in exercise 9-2,K&B -Threads
synchronizing a block of code
threads ... from K&B
about synchronizing
Threads Doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:07:11.