Thread synchronization in java
Thread synchronization:
Every object in Java code has one lock, which is useful for ensuring that only one thread accesses critical code in the object at a time. This synchronization helps prevent the object's state from getting corrupted. If a thread has obtained the lock, no other thread can enter the synchronized code until the lock is released. When the thread holding the lock exits the synchronized code, the lock is released. Now another thread can get the object's lock and execute the synchronized code. If a thread tries to get the lock of an object when another thread has the lock, the thread goes into a blocked state until the lock is released.
Synchronized You can use the synchronized keyword to declare a method as synchronized. This method cannot be accessed by multiple threads simultaneously. The synchronized keyword can also be used to mark a block of code as synchronized. For this, the argument passed should be the object whose lock you want to synchronize on. The syntax is shown below:
Synchronized return_type method() {
// here add statements to be synchronized
}
wait(), notify(), and notifyAll()
The wait(), notify(), and notifyAll() methods are defined in the java.lang.Object class.
When the wait() method is invoked, a thread releases the lock on an object and moves from the running state to the waiting state. The notify() method is used to signal one of the threads waiting on the object to return to the runnable state. It is not possible to specify which of the waiting threads should be made runnable. The notifyAll() method causes all the waiting threads for an object to return to the runnable state.
A thread can invoke wait() or notify() on a particular object only if it currently holds the lock on that object. wait(), notify(), and notifyAll() should be called only from within the synchronized code.
Code Example:
Every object in Java code has one lock, which is useful for ensuring that only one thread accesses critical code in the object at a time. This synchronization helps prevent the object's state from getting corrupted. If a thread has obtained the lock, no other thread can enter the synchronized code until the lock is released. When the thread holding the lock exits the synchronized code, the lock is released. Now another thread can get the object's lock and execute the synchronized code. If a thread tries to get the lock of an object when another thread has the lock, the thread goes into a blocked state until the lock is released.
Synchronized You can use the synchronized keyword to declare a method as synchronized. This method cannot be accessed by multiple threads simultaneously. The synchronized keyword can also be used to mark a block of code as synchronized. For this, the argument passed should be the object whose lock you want to synchronize on. The syntax is shown below:
Synchronized return_type method() {
// here add statements to be synchronized
}
wait(), notify(), and notifyAll()
The wait(), notify(), and notifyAll() methods are defined in the java.lang.Object class.
When the wait() method is invoked, a thread releases the lock on an object and moves from the running state to the waiting state. The notify() method is used to signal one of the threads waiting on the object to return to the runnable state. It is not possible to specify which of the waiting threads should be made runnable. The notifyAll() method causes all the waiting threads for an object to return to the runnable state.
A thread can invoke wait() or notify() on a particular object only if it currently holds the lock on that object. wait(), notify(), and notifyAll() should be called only from within the synchronized code.
Code Example:
package
aurangzeb.utk.com;
public class
WithSynchronizedKeyword {
// a simple
class for printing a string message on screen
class Printer {
//
synchronized method !! onlly one thread can execute it at a time
synchronized void printMsg(String
msg) {
System.out.print("[ --
");
System.out.println(msg + " ");
delay(500);
}
// method that
makes the thread sleep for input number of msec
void delay(int msec) {
try {
Thread.sleep(msec);
}
catch
(InterruptedException ie) {
System.out.println("Interrupted");
}
}
}
// asks the
Printer to print a message in a separate thread
class Caller implements Runnable {
String
msg;
Printer
printer;
Thread
t;
public Caller(Printer
printer, String msg) {
this.printer = printer;
this.msg = msg;
t = new Thread(this);
t.start();
}
public void run() {
printer.printMsg(msg);
}
}
//
constructor to create objects of inner classes
public
WithSynchronizedKeyword() {
Printer
printer = new Printer();
Caller
ob1 = new Caller(printer, "Hello");
Caller
ob2 = new Caller(printer, "Synchronized");
Caller
ob3 = new Caller(printer, "World");
}
// main method
public static void main(String[]
args) {
new
WithSynchronizedKeyword();
}
}
