File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
shankara me
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 2
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Santosh Kumar Nayak
Ranch Hand
Posts: 141
Eclipse IDE IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh Kumar Nayak,
Your post was moved to a new topic.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic