Synchronized block
- Eclipse: Oxygen
- Java: 1.8
The synchronized block is the basic synchronization for a certain code that needs to be synchronized. It means that there might be code where no need for synchronization, then due to applying the synchronized method that the execution of the program is slowed down. Therefore, this problem is improved by a synchronized block.
In the following program, if you want to control the synchronization with a specific part of a method that will be synchronized with the object, specify a synchronized block.
Here we use the synchronized keyword in the method declaration, and synchronize the entire method with the class. Synchronized block construct takes method in parentheses.
In this program “this” is used to synchronize only the first part. Because we are making use of the synchronized block on the synchronized method. It reduces the scope of a lock, and the time required to execute both threads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
class Brakets{ public void generate(){ synchronized (this) { for(int i=1; i<=10; i++){ try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(i<=5){ System.out.print("["); }else{ System.out.print("]"); } } System.out.println(); } for(int j=0; j<=10;j++){ try { Thread.sleep(25); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /* Synchronized method approach * Time required: 5750 * * Synchronized block approach * Time required: 3950 */ public class App { public static void main(String[] args) { Brakets braket = new Brakets(); new Thread(new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); for(int i=1; i<=5; i++){ braket.generate(); } long endtTime = System.currentTimeMillis(); System.out.println("Time required for thread 1 was:"+(endtTime-startTime)); } }).start(); new Thread(new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); for(int i=1; i<=5; i++){ braket.generate(); } long endtTime = System.currentTimeMillis(); System.out.println("Time required for thread 2 was:"+(endtTime-startTime)); } }).start(); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] Time required for thread 1 was:1877 Time required for thread 2 was:1977 |
Synchronized block on an object
Whenever we make use of synchronized, the monitor lock is applied. This feature can be used to hide the synchronized block from the outside world. It is recommended to make use of a private object in order to apply the lock.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
class Brakets{ private Object lock = "lock"; public void generate(){ synchronized (lock) { for(int i=1; i<=10; i++){ try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(i<=5){ System.out.print("["); }else{ System.out.print("]"); } } System.out.println(); } for(int j=0; j<=10;j++){ try { Thread.sleep(25); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /* Synchronized method approach * Time required: 5750 * * Synchronized block approach * Time required: 3950 */ public class App { public static void main(String[] args) { Brakets braket = new Brakets(); new Thread(new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); for(int i=1; i<=5; i++){ braket.generate(); } long endtTime = System.currentTimeMillis(); System.out.println("Time required for thread 1 was:"+(endtTime-startTime)); } }).start(); new Thread(new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); for(int i=1; i<=5; i++){ braket.generate(); } long endtTime = System.currentTimeMillis(); System.out.println("Time required for thread 2 was:"+(endtTime-startTime)); } }).start(); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] Time required for thread 1 was:1901 Time required for thread 2 was:2001 |
Contributed by: Poonam Tomar