Synchronization allows for only one thread to access a piece of code or an object at any given moment. This prevents simultaneous requests to access the data by multiple threads. It is a mechanism put in place to control access to te resources which are shared among all the threads. This is done to ensure accuracy of the processes being carried out by threads and so that they dont interfere with each other or also otherwise known as interleaving, all this can lead to the data being corrupted.
Let us try to understand this using an example. Consider there exists a library a the TeckKnowHow Club. Members of the club can borrow books which they find interesting and return them once they have finished reading them. Now considering this library is multithreaded and different threads handle the borrowing and returning of books by various members of the club, it is possible that two threads try to borrow the same book for different users.
This may lead to inconsistencies in the book status and also not make for a very reliable system. In such a case synchronization needs to be introduced to tackle this issue. In simple words synchronization is used to avoid thread interference and concurrency issues.
Thread intereference: When two or more threads try to read/write to/from a single or multiple shared resources, it may lead to unpredictable and incorrect results.
Concurrency issues: When multiple threads execute concurrently and their order of execution influences the result/outcome. A few basic concurrency issues are listed below :
-
- Race Condition
When multiple threads concurrently access the same shared resource, the outcome depends on the order of execution of the threads, hence giving us inconsistent outputs. - Deadlock
When two or more threads are waiting for each other to release a shared resource, may lead to them being unable to proceed. - Starvation
When multiple threads are waiting for each other to release the resource, it may lead to a few threads being unable to access the resource entirely
- Race Condition
The process of gaining exclusive access over a shared resource by a particular thread is called Locking. The lock on the shared resource exists till the process being executed by the thread is completed. When a thread acquires a lock, it means that it has exclusive permission to execute the code within the locked region.
So how can you implement this concept of synchronization in code?
There are two ways in which this can be done:
-
- Using synchronization blocks.
- Using synchronized methods.
Synchromization Blocks
You can write certiain pieces of code inside a synchronization block, which will synchronize it for you. This method allows for more detailed control over synchronization. This is due to the synchronization being applied only to parts of code and not entire functions or methods.
synchronized (object) { // here object refers to the object for which the shared resources will be locked. // code which needs to be synchronized. }
Synchronized Methods
An entire method can be declared synchronized by using the ‘synchronized’ keyword in the mthod declaration. This is used when we want the entire functionality of a method to be synchronized.
synchronized <return type> function name (args) { // functionality of the method. }
Be First to Comment