Synchronization under concurrency control

  • Eclipse: Oxygen
  • Java: 1.8

Synchronization under concurrency control

Java synchronization is a concept where errors can appear in the code if the same entity is shared by two or more threads. Therefore, it can cause different results each time the execution takes place.

When we start two or more threads within a program, there may be a situation in which multiple threads try to access the same resource. Therefore, it is necessary to synchronize the action of several threads and make sure that only one thread can access the resource at a given time.
As you can see in the following program, that may or may not print a counter value in the sequence and every time it produces a different result based on the availability of the CPU for a thread.

Output

The loops in thread1 is over

The loops in thread2 is over

The value of counter is 1191

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu