Copyright

Atomic Variables in Java: Performance & Tutorial

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In Java, atomic variables let us update variables quickly and simultaneously, at the atomic level. This lesson covers the performance benefits of atomic variables, and provides code examples of implementation.

Why Atomic?

In modern computer systems, we have to allow multiple updates at one time. Java calls these lines of code threads. Think of all the identical threads that makeup an article of clothing. Running the same line(s) of code at the same time is called multi-threading. In order to make multiple updates, systems sometimes use a lock methodology. While a variable or database item is being updated, it is locked by the system until the update is complete. This works well, except that a thread will wind up holding up the line.

Locks? Where we're going, we don't need locks! In Java, you can create atomic variables and these don't need locks. Atomic instructions are also called nonblocking statements because they don't have to hold up the line. Instead of having to wait, atomic operations work at a lower (atomic) level of memory and can update the variables much faster.

Performance

When one thread in a program sets a lock, other threads have to wait for it to release. With atomic variables and instructions, this wait time is reduced or eliminated. Let's take a look at two code examples. Both would be acceptable, but you'll get better performance from the atomic variables.

Synchronization

This is the lock-and-release method in Java. It is invoked by declaring a variable as synchronized.


Java synchronization code


Atomic Variables in Action

Although it is doing the same thing (increment the variable), the atomic code is a little different. First, we have it import the utility for concurrent transactions. That is java.util.concurrent.atomic. This utility includes a couple of functions that we'll use. The get utility retrieves the value, incrementAndGet completes the incrementing of the variable.


Java atomic variables in use


Instead of having to wait for the lock to be released, the atomic variable handles multiple updates much faster and simultaneously.

Let's say we have hundreds of players in a game, and we need to continuously update the counter variable. The trick is that it has to kept in synch. It can't be set to 5 by one thread, and then 25 by the next thread that hits it.

Atomic processing to the rescue! The last part of the code below (the stuff within the main function) creates some threads and kicks them off. In this code it's only 4, but we could have 400. The concept is the same. When we run multiple threads, we need to make use of the Runnable class, which is the delivered Java tool that makes multi-threading possible. In our example, we'll create threads manually, but advanced programs may generate them on the fly.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support