Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

example code to show how java synchronized block works

Tags:

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?

like image 442
CaiNiaoCoder Avatar asked Nov 17 '11 03:11

CaiNiaoCoder


People also ask

What is synchronized block in Java with example?

A Synchronized block is a piece of code that can be used to perform synchronization on any specific resource of the method. A Synchronized block is used to lock an object for any shared resource and the scope of a synchronized block is smaller than the synchronized method.

How does synchronized in Java work?

1. Synchronized keyword in Java is used to provide mutually exclusive access to a shared resource with multiple threads in Java. Synchronization in Java guarantees that no two threads can execute a synchronized method which requires the same lock simultaneously or concurrently.

What is synchronization in Java Real Time example?

Synchronization in Java is the process that allows only one thread at a particular time to complete a given task entirely. By default, the JVM gives control to all the threads present in the system to access the shared resource, due to which the system approaches race condition.

What is synchronization with example?

The process of allowing only a single thread to access the shared data or resource at a particular point of time is known as Synchronization. This helps us to protect the data from the access by multiple threads. Java provides the mechanism of synchronization using the synchronized blocks.


2 Answers

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

like image 182
Lucifer Avatar answered Oct 16 '22 05:10

Lucifer


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

like image 41
Mihir Bera Avatar answered Oct 16 '22 04:10

Mihir Bera