aspose file tools*
The moose likes Threads and Synchronization and the fly likes synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized block" Watch "synchronized block" New topic
Author

synchronized block

shankara me
Greenhorn

Joined: Nov 30, 2012
Posts: 14
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?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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!
Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 107
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 420
    
    2

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.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Santosh Kumar Nayak,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized block