Forums Register Login

synchronized (Thread.currentThread())

+Pie Number of slices to send: Send
hi people,

I just wanted to understand that why exactly would we synchronize a current thread object...

Why do we do this

synchronized (Thread.currentThread())
+Pie Number of slices to send: Send
I don't think you should ever do that. If each Thread is synchronizing on itself, then the there is no synchronization going on. You might use it for signaling:

But even this, I don't think is a good idea since there can be un-predictable notifications associated with using the Thread object itself as a Lock.

I don't see any good reason to use the synchronized(Thread.currentThread()). Rather lock on the data you are trying to protect or on some other purpose-built object you have more control over.
+Pie Number of slices to send: Send
Synchronizing threads on itself...
I think I did this sometime before when I was developing games on the j2me platform. Can you elaborate as to where you are using it?
+Pie Number of slices to send: Send
I wish someone could elaborate on the purpose and functionality of synchronizing the current thread object as this construct can be found in one of the questions of OCJP test. I cannot publish the actual question due copyright infringement (which is otherwise a perfect excuse for them to get away with bullshit on those tests)

+Pie Number of slices to send: Send
There is no reason to do that. You should realize that the Code snippets used in those exams are are not Meant to show you how to code, they are meant to show problems so you can find them.
+Pie Number of slices to send: Send
 

Frederick Koko wrote:I wish someone could elaborate on the purpose and functionality of synchronizing the current thread object as this construct can be found in one of the questions of OCJP test. I cannot publish the actual question due copyright infringement (which is otherwise a perfect excuse for them to get away with bullshit on those tests)




And yet, you decided to do this again.... Violating copyrights is *not* allowed on the ranch. It is illegal. Please stop doing this.

Henry
What? What, what, what? What what tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2990 times.
Similar Threads
Hashtable methods
threads problem
Getting reference to active threads
Chapter 9: syncronized(this)
wait() releases all locks?
IllegalMonitorStateException
More...

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