Synchronized Technique: BoyFriend Threads and GirlFriend Object [Video]

When a technique is synchronized, just one thread can enter that object’s technique at a given cut-off date. If some other thread tries to enter the synchronized technique, it would NOT be allowed to enter. It will likely be put into the BLOCKED state.

On this publish, let’s study a bit extra particulars concerning the synchronized technique.

Boys and Girls with heart balloons

Java Synchronized Technique Instance

It’s all the time simple to study with an instance. Right here is an attention-grabbing program that I’ve put collectively which facilitates us to know the synchronized technique habits higher:

01: public class SynchronizationDemo  
02:        
03:    personal static class BoyFriendThread extends Thread  
04:        
05:       @Override 
06:       public void run()  
07:           
08:          girlFriend.meet(); 
09:        
10:     
11:  
12:    personal static GirlFriend girlFriend = new GirlFriend(); 
13:     
14:    public static void fundamental(String[] args)  
15:        
16:       for (int counter = 0; counter < 10; ++counter)  
17:           
18:          BoyFriendThread fThread = new BoyFriendThread(); 
19:          fThread.setName("BoyFriend-" + counter); 
20:          fThread.begin(); 
21:        
22:     
23: 
01: public class GirlFriend  
02:    
03:   public void meet()  
04:       
05:      String threadName = Thread.currentThread().getName(); 
06:      System.out.println(threadName + " assembly began!"); 
07:      System.out.println(threadName + " assembly ended!!");          
08:       
09: 

On this program, there are two courses: 

1. SynchronizationDemo

On this class, you’ll be able to discover that we’re beginning 10 BoyFriendThread from traces #18-20. Every BoyFriendThread is invoking meet() technique on the GirlFriend object in line #8.

2. GirlFriend 

On this class, there is just one meet() technique. This technique prints the title of the Boyfriend thread title and assembly began! and assembly stopped!!

When the SynchronizationDemo program is executed, it prints the next because the output: 

01: BoyFriend-0 assembly began! 
02: BoyFriend-8 assembly began! 
03: BoyFriend-1 assembly began! 
04: BoyFriend-0 assembly ended!! 
05: BoyFriend-4 assembly began! 
06: BoyFriend-3 assembly began! 
07: BoyFriend-3 assembly ended!! 
08: BoyFriend-2 assembly began! 
09: BoyFriend-8 assembly ended!! 
10: BoyFriend-2 assembly ended!! 
11: BoyFriend-5 assembly began! 
12: BoyFriend-6 assembly began! 
13: BoyFriend-6 assembly ended!! 
14: BoyFriend-7 assembly began! 
15: BoyFriend-7 assembly ended!! 
16: BoyFriend-9 assembly began! 
17: BoyFriend-1 assembly ended!! 
18: BoyFriend-4 assembly ended!! 
19: BoyFriend-9 assembly ended!! 
20: BoyFriend-5 assembly ended!!

You may discover that the assembly began! and assembly stopped!! statements of every thread are usually not printed consecutively. For instance, on line #1, a gathering with BoyFriend-0 began, and whereas that assembly was in progress, conferences with BoyFriend-8 (line #2) and BoyFriend-1 (line #3) additionally began. It is just after that that the assembly with BoyFriend-0 ends. These sorts of combined conferences proceed all through this system. If this occurs in the true world, then our GirlFriend object can be in bother. We should always save her from this bother. Solely after her assembly with one boyfriend completes, the subsequent assembly ought to resume. 

That is the place synchronization comes to assist.

Let’s change the meet() technique within the GirlFriend object to be synchronized, and execute this system as soon as once more.

01: public class GirlFriend  
02:    
03:   public synchronized void meet()  
04:       
05:      String threadName = Thread.currentThread().getName(); 
06:      System.out.println(threadName + " assembly began!"); 
07:      System.out.println(threadName + " assembly ended!!");          
08:       
09: 

Discover that in line #3, the synchronized key phrase is launched within the meet() technique. After we executed the identical program as soon as once more, under is the consequence we bought:

01: BoyFriend-2 assembly began! 
02: BoyFriend-2 assembly ended!! 
03: BoyFriend-0 assembly began! 
04: BoyFriend-0 assembly ended!! 
05: BoyFriend-5 assembly began! 
06: BoyFriend-5 assembly ended!! 
07: BoyFriend-8 assembly began! 
08: BoyFriend-8 assembly ended!! 
09: BoyFriend-9 assembly began! 
10: BoyFriend-9 assembly ended!! 
11: BoyFriend-6 assembly began! 
12: BoyFriend-6 assembly ended!! 
13: BoyFriend-7 assembly began! 
14: BoyFriend-7 assembly ended!! 
15: BoyFriend-4 assembly began! 
16: BoyFriend-4 assembly ended!! 
17: BoyFriend-3 assembly began! 
18: BoyFriend-3 assembly ended!! 
19: BoyFriend-1 assembly began! 
20: BoyFriend-1 assembly ended!!

Bingo!! Now you’ll be able to see every assembly with a boyfriend began and ended sequentially. Solely after finishing a gathering with BoyFriend-0 does a gathering with BoyFriend-5 begin. This could make the GirlFriend object fairly blissful, as she will give attention to one Boyfriend at a time. 

Observe: A few of you may surprise why BoyFriend threads are usually not assembly the GirlFriend object in sequential order. The reply to this query can be coming quickly in an upcoming publish.

How Does the Synchronized Technique Work in Java?

While you make a technique synchronized, just one thread can be allowed to execute that technique at a given cut-off date. When a thread enters a synchronized technique, it acquires the lock of the thing. Solely after this thread releases the lock, different threads can be allowed to enter the synchronized technique. 

Say the BoyFriend-0 thread is executing the meet() technique; solely after this thread exits the strategy, different threads can be allowed to enter the meet() technique, till then all different threads which try to invoke the meet() technique can be put to the BLOCKED thread state.

Threads Habits When a Technique Is Synchronized

To substantiate this concept, we executed the above program and captured the thread dump utilizing the open-source script yCrash. We analyzed the thread dump utilizing the fastThread instrument. Right here is the generated thread dump analysis report of this straightforward program. Under is the excerpt from the thread dump evaluation report: 

fastThread tool reporting 9 threads are in BLOCKED state

Fig 1: fastThread instrument reporting 9 threads are in BLOCKED state

fastThread tool reporting 9 threads to be in BLOCKED state when accessing GirlFriend Object

Fig 2: fastThread instrument reporting 9 threads to be in BLOCKED state when accessing GirlFriend Object

fastThread instrument reviews the full variety of blocked threads and a transitive graph to point the place they’re BLOCKED. On this instance (Fig 1), the instrument reported that 9 threads are in a BLOCKED state. From the transitive graph (Fig 2), you’ll be able to discover that BoyFriend2 is obstructing the remaining 9 BoyFriend threads. When clicking on the thread names, you’ll be able to see its full stack hint. 

Under is the stack hint of one of many BoyFriend threads which is BLOCKED:

01: java.lang.Thread.State: BLOCKED (on object monitor) 
02: at study.synchornized.GirlFriend.meet(GirlFriend.java:7) 
03: - ready to lock <0x0000000714173850> (a study.synchornized.GirlFriend) 
04: at study.synchornized.SynchronizationDemo$BoyFriendThread.run(SynchronizationDemo.java:10) 
05: Locked ownable synchronizers: 
06: - None

You may see that this BoyFriend thread is put right into a BLOCKED state when it’s making an attempt to execute the meet() technique (as reported in line #2).

Video

To see the visible walk-through of this publish, click on under:

Conclusion

On this publish, we realized concerning the fundamentals of the synchronized technique. If you wish to study extra particulars concerning the synchronized technique, keep tuned.