File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes synchronized (Thread.currentThread()) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized (Thread.currentThread())" Watch "synchronized (Thread.currentThread())" New topic
Author

synchronized (Thread.currentThread())

Gaurav .Singh
Greenhorn

Joined: Apr 06, 2010
Posts: 8
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

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

Joined: May 04, 2013
Posts: 3
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized (Thread.currentThread())