Why do we need synchronization in Java, and what happens if we are not using synchronization in Java for a multi-threading environment?

We use Synchronization in Java whenever we want only thread to access particular resource at a particular time. If more than 1 thread tries to accessing same resource at a same time then it will allow only the first thread to access that resource. Once completed then only second thread can use that resource.

We use Synchronization in Java to avoid dead locks. Dead lock is the condition in java when 2 threads were accessing same resource and they stuck in a way that not the first thread can be completed nor the second thread can be completed.

Be the first to comment

Leave a Reply

Your email address will not be published.


*