Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How synchronized keyword in java have been implemented?

I'm reading operating system and I came across several problems for inter-process communication. These can be solved by using monitor concepts which java provide via synchronized keyword.

I wish to know how synchronized keyword have been implemented? I tried to look at the source but I couldn't able to find it. Are synchronized are using system calls like down up ( which semaphore uses basically) to monitor the locks?

Does JVM help in this process?

I'm a novice in Java, I wish to know how things works before I go into thread concepts in java.

Thanks in advance.

like image 438
sriram Avatar asked Sep 11 '12 07:09

sriram


People also ask

How does synchronized keyword work in Java?

The synchronized keyword prevents concurrent access to a block of code or object by multiple threads. All the methods of Hashtable are synchronized , so only one thread can execute any of them at a time.

How is thread synchronization implemented in Java?

This is possible by using an object locking concept. Thread Synchronization is a process of allowing only one thread to use the object when multiple threads are trying to use the particular object at the same time. To achieve this Thread Synchronization we have to use a java keyword or modifier called “synchronized”.

How does the synchronized keyword work?

The synchronized keyword can be used to ensure that only one thread at a time executes a particular section of code. This is a simple way to prevent race conditions, which occur when several threads change shared data at the same time in a way that leads to incorrect results.

Which keyword is used for implementing synchronization?

Which keyword is used for using the synchronization features defined by the Monitor class? Explanation: The C# keyword lock is really just shorthand for using the synchronization features defined by the Monitor class, which is defined in the System.


1 Answers

How synchronized is implemented is not defined, only how it works.

In many JVMs, what it does is quite complicated to optimise its behaviour (for example it tries to avoid making system calls as these are relatively slow) For example the JIT can combine or eliminate locking with the synchronized keyword if it determines this can be done.

like image 63
Peter Lawrey Avatar answered Sep 24 '22 13:09

Peter Lawrey