• 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 block

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The general form of synchronized block is

Here obj means object of table class or object of any class.
If we write this in place of obj ,then an object of table class can’t access the synchronized block from two different threads.
MYDOUBTS:
1)Is it possible to write name of object other than table class in place of obj?
2)If write other object’s name other than table class in place of obj,what it means?
3)Then which object can’t access the synchronized block simultaneously?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects don't have names. Variables do.

Synchronization always works the same way an always does the same thing, no matter what object you're syncing on and no matter if it's a synced block or a synced instance method or a synced class method.

All that syncing does is this:*

1. Stop and wait for the specified object's lock to be available (not held by any other thread) before entering the synced block or method.

2. Obtain that lock.

3. Release the lock temporarily if wait() is called, and reacquire it before continuing on after wait.

4. Release the lock upon exiting the sync block or method.


That's it. It's that simple, and it's always the same. Syncing just says "I know hold this lock, and no other thread can hold it until I release it (and therefore cannot enter any block synced on the same lock)." Note that syncing is about which thread can execute a block of code. It has nothing to do with which object can access anything.



*Syncing also has some memory barrier effects, but this is all it does in terms of mutual exclusion.
 
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

Jeff Verdegan wrote:Note that syncing is about which thread can execute a block of code. It has nothing to do with which object can access anything.


That is worth highlighting and repeating!
 
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we have a large synchronized block and number of multiple(huge amount) threads running in the background. Each thread has to execute through the synchronized block then eventually it takes huge time.

What can we do in order to reduce the time consumption ?

Regards,
Santosh.
 
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

Santosh Kumar Nayak wrote:When we have a large synchronized block and number of multiple(huge amount) threads running in the background. Each thread has to execute through the synchronized block then eventually it takes huge time.

What can we do in order to reduce the time consumption ?



Even though you phrased in in a form of a thread question -- technically, it is not a thread question.

If your algorithm is taking a long time to run, you will need to refactor/redesign so that it doesn't take as long to run. If your algorithm is requiring being synchronized, you will need to refactor/redesign so that it doesn't (or less of it) need to be synchronized.

Henry
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If profiling shows the block to be time critical (big if) and you have the CPU resources to benefit from concurrent access , then you could consider if read and write locks were appropriate i.e. if you mostly read (an only read / don't modify in this particular block) so in this example a print (don't modify the data) then read locks would allow multiple threads to access the same code at the same time.

Google java ReadLock and WriteLock for more info.
 
Henry Wong
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
Santosh Kumar Nayak,
Your post was moved to a new topic.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic