How To Use Synchronization in Java PDF
How To Use Synchronization in Java PDF
You have 2 free stories left this month. Sign up and get an extra one for free.
In this article, you will learn why we need Java synchronization, how to
write synchronized code, and more important points about
synchronization.
. . .
Method Synchronization
You can declare a synchronized method in Java by using the
synchronized keyword. When a thread invokes a synchronized method, it
automatically locks an object when it has shared data and releases it
when the thread completes its task. So, if one thread runs the
synchronized method, all other threads that invoke the synchronized
method on the same object will have to wait until the first thread is
finished. Let’s see an example.
1 class Apple {
2 synchronized public void getApple() {
3 for (int i = 0; i < 3; i++) {
4 System.out.println(i);
5 try {
6 Thread.sleep(400);
7 }
8 catch (Exception e) {
9 System.out.println(e);
10 }
11 }
12 }
13 }
14
15 class Tree extends Thread {
16
17 Apple apple;
18
19 Tree (Apple apple) {
20 this.apple = apple;
21 }
22
23 @Override
24 public void run() {
25 apple.getApple();
26 }
27 }
28
29 public class SyncEx1 {
30 public static void main(String[] args) {
31
32 Apple obj = new Apple(); //Object of Apple class that is shared amoung threads
33
34 Tree tree1 = new Tree(obj);
35 Tree tree2 = new Tree(obj);
36
37 tree1.start();
38 tree2.start();
39 }
40 }
Block Synchronization
Suppose you have 50 lines of code in your method, but you just want to
synchronize 5 lines. The synchronized block allows you to synchronize
any specific part of the method. It tells the JVM to allow only one thread
to access the specified piece of code at a time.
1 import java.util.*;
2 public class SyncEx2{
3
4 public static void main(String []args){
5 try{
6 List<String> al = new ArrayList<>();
7 List movieList = Collections.synchronizedList(al);
8 movieList.add("StarWars");
9 movieList.add("Avengers");
10 movieList.add("Inception");
11
12 synchronized(movieList) { // Synchronized block
13 Iterator i = movieList.iterator();
14 while (i.hasNext())
15 System.out.println(i.next()); // Print Synchronized movie list
16 }
17
18 } catch (IllegalArgumentException e) {
19 System.out.println("Exception thrown : " + e);
20 }
21 }
22 }
. . .
and notifyAll() .
You can use the wait() method to set the current thread to release
the lock and wait until another thread invokes the notify() method
or notifyAll() method for this object, or until the specified time has
elapsed.
The notify() method will choose and wakeup a single thread that is
waiting on this object, and the notifyAll() method will wake up all
the threads that are waiting on this object’s monitor.
. . .
431 claps
WRIT T EN BY
How to use Generics in 7 T hings to Build When React Antipatterns to AWS CDK for Beginners
Java You Feel Bored as a Avoid Rani Lian in Level Up Coding
Manusha Chethiyawardhana in Programmer John Au-Yeung in Level Up
Level Up Coding Daan in Level Up Coding Coding