WHAT ARE THREADS ?. Threads are like "mini-processes" within a program, capable of executing tasks independently..
Thread synchronization in java is a way of programming several threads to carry out independent tasks easily. It is capable of controlling access to multiple threads to a particular shared resource..
Problems without synchronization. Race conditions:.
LET’S HAVE AN EXAMPLE:. Imagine a busy kitchen with multiple chefs working on different dishes:.
Thread synchronization- It refers to the concept where only one thread is executed at a time while other threads are in the waiting state. This process is called thread synchronization..
MUTUAL EXCLUSION SYNCHRONIZED BLOCK SYNCHRONIZED METHOD STATIC SYNCHRONISATION.
Synchronized method. Syntax: synchronized public void methodName().
Synchronized Block If a block is declared as synchronized then the code which is written inside a method is only executed instead of the whole code. It is used when sequential access to code is required..
Static Synchronization The method is declared static in this case. It means that lock is applied to the class instead of an object and only one thread will access that class at a time..
Hold locks for the shortest time possible Avoid nested synchronization blocks to reduce contention. Synchronize only critical sections of code that access shared resources. Prefer high-level concurrency constructs when appropriate. Test thoroughly for thread safety issues..
APPLICATIONS Embedded Systems Scientific Computing Graphical User Interfaces Web Servers Multimedia Applications Network Applications.