• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized (Thread.currentThread())

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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())
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
security forum advocate
Posts: 236
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Those are the largest trousers in the world! Especially when next to this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic