Static synchronization
- Eclipse: Oxygen
- Java: 1.8
The concept of static synchronization is shown here. For the issue regarding the inconsistency caused due to the multiple objects accessing the synchronized block causes an error, hence by declaring the function as static, we can avoid the same. Therefore, we can directly access the methods by the class name.
As you can see, we have class Brackets. Inside the class, there is a generate() method. We want one thread to finish printing the message within the method then only another thread should start executing the method.
Here we have created two objects bracket1 and bracket2. These two objects are calling the generate() method.
Here we have used a non-static synchronized method. Generally, this method holds the lock on the current object and it can run at the same time. It can produce inconsistency.
The following program demonstrates the output without using static synchronization.
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 |
class Brackets { synchronized public void generate() { for (int i = 1; i <= 10; i++) { if (i <= 5) { System.out.print("["); } else { System.out.print("]"); } } System.out.println(); } } public class App { public static void main(String[] args) { Brackets bracket1 = new Brackets(); Brackets bracket2 = new Brackets(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 5; i++) { bracket1.generate(); } } }).start(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 5; i++) { bracket2.generate(); } } }).start(); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[[[[[]]][[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] ]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] [[[[[]]]]] |
Sometimes making use of a single object is not possible. Then we should go for static synchronization.
Whenever we make use of static elements as synchronized, the lock would be based on the class directly.
The following program demonstrates the synchronization using the static synchronized method.
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 |
package org.studyeasy; class Brackets{ synchronized static public void generate(String threadCode){ for(int i=1; i<=10; i++){ if(i<=5){ System.out.print("["); }else{ System.out.print("]"); } } System.out.println("Generated by thread: "+threadCode); } } public class App { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { for(int i=1; i<=5; i++){ Brackets.generate("Thread 1"); } } }).start(); new Thread(new Runnable() { @Override public void run() { for(int i=1; i<=5; i++){ Brackets.generate("Thread 2"); } } }).start(); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[[[[[]]]]]Generated by thread: Thread 2 [[[[[]]]]]Generated by thread: Thread 2 [[[[[]]]]]Generated by thread: Thread 2 [[[[[]]]]]Generated by thread: Thread 2 [[[[[]]]]]Generated by thread: Thread 2 [[[[[]]]]]Generated by thread: Thread 1 [[[[[]]]]]Generated by thread: Thread 1 [[[[[]]]]]Generated by thread: Thread 1 [[[[[]]]]]Generated by thread: Thread 1 [[[[[]]]]]Generated by thread: Thread 1 |
Contributed by: Poonam Tomar