25

I am learning java multi-threading, I found it's hard to understand how synchronized block works:

 synchronized(Object o){
     // do something
    }

please give some example code that can show me the Object o is blocked. As how I understand this, accessing object o from another thread will be blocked while the synchronized block is being excuted?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
CaiNiaoCoder
  • 3,269
  • 9
  • 52
  • 82

3 Answers3

14

Synchronization in Java is an important concept since Java is a multi-threaded language where multiple threads run in parallel to complete program execution. In multi-threaded environment synchronization of java object or synchronization of java class becomes extremely important. Synchronization in Java is possible by using java keyword "synchronized" and "volatile”. Concurrent access of shared objects in Java introduces to kind of errors: thread interference and memory consistency errors and to avoid these errors you need to properly synchronize your java object to allow mutual exclusive access of critical section to two threads.

Read more: http://javarevisited.blogspot.com/2011/04/synchronization-in-java-synchronized.html#ixzz2LOWwnCjH

Please Look at this Example

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Lucifer
  • 29,392
  • 25
  • 90
  • 143
1

Synchronization describes that if an object or a block is declared as synchronized then only one process can access that object or block at a time.No other process can take the object or block until it is available .Internally each object has one flag named "lock" has two state set and reset. when a process requests one object then it is checked whether the lock value is set or reset. Depending on that one object is available to a process in synchronization . For better understanding with example you can see this link. enter link description here

Mihir Bera
  • 11
  • 2
  • While this might answer the question, the question is already answered. Please elaborate on how your answer is different do the one(s) already here (and accepted) - or how it extends the them. – T3 H40 Nov 12 '15 at 06:34
0

As Most of the answers have covered what synchronized means i want to add one extra point which isn't mentioned. Synchronizing a method or enclosing a block with synchronized ensures that the operation/set of operations execute as a single atomic operation,to be precise when one thread is executing synchronize block on an object no other thread can enter the block until the thread one completes its execution and releases the lock it holds which it gets while entering the block.

So synchronize block ensure atomicity of bunch of code statements.

unlike what @lucifier specified ,Synchronizing and volatile doesn't serve the same purpose ,volatile is meant to ensure that two threads communicate with each other and get the most update value from the main memory instead of accesing a value from individual cache.it also ensures "happens before " behavior for a execution.

For example defining a variable as volatile(volatile int i=10;) an performing a increment operation (i++;)in an unsynchronized method doesnt give the same behavior when (i++) is enclosed in a synchronized block.

k.explorer
  • 291
  • 6
  • 19