Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized (Thread.currentThread())

 
Gaurav .Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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())
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sai Hegde
security forum advocate
Ranch Hand
Posts: 212
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Frederick Koko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic