Synchronization In Java Part 2

BIS
2 min readJan 25, 2020

In my previous article, I explained usages of volatile keyword in Java, but some time for right synchronization just volatile keyword may not be sufficient. I will explain the usages of Java synchronized keyword in this article.

In the previous example, we used a short index variable where we can set and reset the values. How about using incremental, decremental, post and pre incremental and decremental. This will not work with with the just volatile keyword.

example

public volatile short index =0;

if two threads want to perform index++ and index — , what will be the consequence. Lets first clarify index++ and index — as follows:

index = index + 1; // Equivalent of index++

index = index — 1; // Equivalent of index —

Now on the outset, it appears simple but how CPU performs internal varies from CPU to CPU. The volatile keywords function effectively when the operation is being performed in a single CPU cycle. here in our example, it will take more than one CPU cycle. Hence we need a solution which guarantees automaticity.

Java has keyword synchronized which can be used to help us here.

We can rewrite our example as follows:

synchronized {

index++;

}

synchronized {

index — ;

}

This will guarantee us atomicity and index will be synchronized correctly in the multi-threaded application. I will cover the synchronized keyword in part 3 of the Java Synchronization article.

Similarly, Business Integration Software provides sets of synchronisation techniques for automaticity in our Online Exam Software.

Online Test Software

Exam Software

Examination Software

Question Bank Software

Online Exam Software

Online Test System

Online Examination Software

Online Assessment System

Assessment Management Software

Multiple Choice Question Software

Examination Management Software

Exam Management Software

Examination Management System

Exam Management System

Online Examination System

MCQ Software

Online Assessment Software

Test Management Software

Online Assessment Software

Exam System

Examination System

RHEED Software

MLM Software

Service Management Software

Online Timesheet Software

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

BIS
BIS

Written by BIS

Specialist in Cloud Native development

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations