![]() ![]() Now, let us discuss some basic concepts related to synchronization in Java. ![]() Going forward, we will discuss synchronization in Java in detail in this tutorial. When a monitor is being accessed by a particular thread, the monitor is locked and all the other threads trying to enter the monitor are suspended until the accessing thread finishes and releases the lock. When a thread gets the lock, then, we can say the thread has entered the monitor. A monitor can be accessed by only one thread at a time. We use a concept called Monitors to implement synchronization. To avoid these errors we need to go for synchronization of shared resources so that the access to these resources is mutually exclusive. Most of the time, concurrent access to shared resources in Java may introduce errors like “Memory inconsistency” and “thread interference”. In this case, JVM ensures that Java synchronized code is executed by one thread at a time. In this case, we do not need to synchronize the resource. If the resource is immutable, then the threads will only read the resource either concurrently or individually. We need synchronization when the shared object or resource is mutable. We use keywords “synchronized” and “volatile” to achieve Synchronization in Java Multi-threading Without SynchronizationĪs Java is a multi_threaded language, thread synchronization has a lot of importance in Java as multiple threads execute in parallel in an application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |