Back to Top

Sunday, 15 January 2012

JAVA : Describe synchronization in respect to multithreading.

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. synchronization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors. So, its the mechanism that ensures that only one thread is accessed the resources at a time.


How to achieve synchronization in java?

In java, synchronization can be achieved by using the 
synchronized keyword. 
It can be used in 2 ways.
1. on a method signature: We can declare synchronized keyword in method signature, therefore, that method will be thread safe in multi-threaded environment. That means, only, one thread at a time can access this method.
Below, method execute() is synchronized, so it is can be accessed by one thread at a time.
       
synchronized void execute(){
  // do something
}
       
2. Write synchronized block: Secondly, we can write a synchronized block, and that block will be synchronized.

synchronized(this){
  // do something
}


The above code, will lock the object itself, when this piece of code has been executed by a thread.

3 Responses to “ JAVA : Describe synchronization in respect to multithreading. ”

Naviya Nair said...
24 October 2016 at 15:01

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai


Naviya Nair said...
24 October 2016 at 15:28

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai


jazz said...
21 March 2017 at 16:26

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your..
Vmware Training in Chennai


Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs