Identify Atomic and Non-Atomic Operations

Identify Atomic and Non-Atomic Operations

In Java, the reading and writing of 32-bit or smaller quantities are guaranteed to be atomic. By atomic we mean each action takes place in one step and cannot be interrupted. Thus, when we have multithreaded applications, the read and write operations are thread-safe and need not be made synchronized.

For example, the following code is thread safe:

 public class ThreadSafe{    private int x;    public void setX(int x)    {         this.x = x;    }}

However, we should be careful to note that the guarentee applies only to reading and writing. For eg. check the following code snippet:

 public void increment(){        ++this.value;} 

Although the above snippet looks to be atomic operation, it is not. The above instruction actually consists of three instructions:

1) Read current setting of ‘value’.
2) Increment the setting.
3) Write the new setting back.

Thus, the above code is not thread-safe. To add to the problem, this mistake is not very easily caught during testing due to two reasons:

1) Threading bugs are difficult to detect and are time consuming.
2) This code snippet might translate into a single instruction on some CPUs and thus work correctly. The problem might arise when tested with other JVMs.

The above code can be made threadsafe by simply synchronizing the code as follows:

 public synchronized void increment(){        ++this.value;} 
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing