Movatterモバイル変換


[0]ホーム

URL:


Open In App

Synchronization: 

Synchronization is a modifier that is used for the method and blocks only. With the help of a synchronized modifier, we can restrict a shared resource to be accessed only by one thread. When two or more threads need access to shared resources, there is some loss of data i.e. data inconsistency. The process by which we can achieve data consistency between multiple threads is called Synchronization.

Why do you need Synchronization? 

Let us assume if you have two threads that are reading and writing to the same 'resource'. Suppose there is a variable named geek, and you want that at one time only one thread should access the variable(atomic way). But Without the synchronized keyword, your thread 1 may not see the changes thread 2 made to geek, or worse, it may only be half changed that cause the data inconsistency problem. This would not be what you logically expect. The tool needed to prevent these errors is synchronization.

In synchronization, there are two types of locks on threads:  

  • Object-level lock:Every object in java has a unique lock. Whenever we are using a synchronized keyword, then only the lock concept will come into the picture. If a thread wants to execute then synchronized method on the given object. First, it has to get a lock-in that object. Once the thread got the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and release lock internally is taken care of by JVM and the programmer is not responsible for these activities. Let's have a look at the below program to understand the object level lock:
JAVA
// Java program to illustrate// Object lock conceptclassGeekimplementsRunnable{publicvoidrun(){Lock();}publicvoidLock(){System.out.println(Thread.currentThread().getName());synchronized(this){System.out.println("in block "+Thread.currentThread().getName());System.out.println("in block "+Thread.currentThread().getName()+" end");}}publicstaticvoidmain(String[]args){Geekg=newGeek();Threadt1=newThread(g);Threadt2=newThread(g);Geekg1=newGeek();Threadt3=newThread(g1);t1.setName("t1");t2.setName("t2");t3.setName("t3");t1.start();t2.start();t3.start();}}

Output
t1t3t2in block t3in block t1in block t3 endin block t1 endin block t2in block t2 end
  • Class level lock:Every class in Java has a unique lock which is nothing but a class level lock. If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Once a thread got the class level lock, then it is allowed to execute any static synchronized method of that class. Once method execution completes automatically thread releases the lock. Let's look at the below program for better understanding:
JAVA
// Java program to illustrate class level lockclassGeekimplementsRunnable{publicvoidrun(){Lock();}publicvoidLock(){System.out.println(Thread.currentThread().getName());synchronized(Geek.class){System.out.println("in block "+Thread.currentThread().getName());System.out.println("in block "+Thread.currentThread().getName()+" end");}}publicstaticvoidmain(String[]args){Geekg1=newGeek();Threadt1=newThread(g1);Threadt2=newThread(g1);Geekg2=newGeek();Threadt3=newThread(g2);t1.setName("t1");t2.setName("t2");t3.setName("t3");t1.start();t2.start();t3.start();}}

Output
t1t2t3in block t1in block t1 endin block t3in block t3 endin block t2in block t2 end

Reference:https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html
 

 


B

Bishal Dubey
Improve

B

Bishal Dubey
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp