DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Exploring Exciting New Features in Java 17 With Examples
  • Python Variables Declaration
  • Linked List in Data Structures and Algorithms
  • Providing Enum Consistency Between Application and Data

Trending

  • The Smart Way to Talk to Your Database: Why Hybrid API + NL2SQL Wins
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Integration Isn’t a Task — It’s an Architectural Discipline
  1. DZone
  2. Data Engineering
  3. Data
  4. Java Thread Programming (Part 3)

Java Thread Programming (Part 3)

Tip: We can only ask the Java thread to execute a piece of code, but we cannot guarantee the execution order of multiple threads!

By 
A N M Bazlur Rahman user avatar
A N M Bazlur Rahman
DZone Core CORE ·
Aug. 16, 22 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
6.7K Views

Join the DZone community and get the full member experience.

Join For Free

Continuing from part 2, let’s start this article with a bit of context first (and if you don’t like reading text, you can skip this introduction, and go directly to the section below where I discuss pieces of code).

Context

  • When we start an application program, the operating system creates a process.
  • Each process has a unique id (we call it a PID) and a memory boundary.
  • A process allocates its required memory from the main memory, and it manipulates data within a boundary.
  • No other process can access the allocated memory that is already acquired by a process.
  • It works like a sandbox, and in that way, avoids processes stepping on one another's feet.
  • Ideally, we can have many small processes to run multiple things simultaneously on our computers and let the operating system's scheduler schedule them as it sees fit.
  • In fact, this is how it was done before the development of threads. However, when we want to do large pieces of work, breaking them into smaller pieces, we need to accumulate them once they are finished.
  • And not all tiny pieces can be independent, some of them must rely on each other, so we need to share information amongst them.
  • To do that, we use inter-process communication. The problem with this idea is that having too many processes on a computer and then communicating with each other isn’t cheap. And precisely that is where the notion of threads comes into the picture.

The idea of the thread is that a process can have many tiny processes within itself. These small processes can share the memory space that a process acquires. These little processes are called "threads." So the bottom line is that threads are independent execution environments in the CPU and share the same memory space. That allows them faster memory access and better performance.

That seems to be a good idea. However, it comes with many problems alongside its benefits. Let’s discuss some of those problems and how we can deal with them. But don’t get discouraged; the benefits still outweigh the problems!

Code

Let’s begin by running a piece of code.

Java
 
package com.bazlur.threads;

public class FoojayPlayground {
 private static boolean running = false;

 public static void main(String[] args) {
  var t1 = new Thread(() -> {
   while (!running) {
   }
   System.out.println("Foojay.io");
  });

  var t2 = new Thread(() -> {
   running = true;
   System.out.println("I love ");
  });

  t1.start();
  t2.start();
 }
}

The above piece of code is reasonably straightforward. We have created two threads. Both of them share a variable named "running". There is a while loop inside the first thread. The loop will keep running while the variable is false, which means this thread will continue to execute the loop unless the variable is changed. Once the loop breaks, it prints “Foojay.io.” The second thread changes the variable and then prints “I love.”

Now the question gets to be, what would be the output?

Outwardly, it seems the output would be the following:

Shell
 
I love
Foojay.io

Well, that’s only one case, because if you run the above code several times, you will see different results. There would be three outcomes of the program, varying on different computers. The reason is, that we can only ask the thread to execute a piece of code, but we cannot guarantee the execution order of multiple threads. Threads are scheduled by the operating system's thread scheduler. We will discuss the thread’s lifecycle in forthcoming articles.

Case 1. The first thread will continue running the loop. In contrast, the second thread will change the variable and immediately print “I love”. Since the variable has now changed, the loop breaks, and it prints “Foojay.io” so that the output is:

Shell
 
I love
Foojay.io

Case 2. The second thread will run first and change the variable and then immediately in the first thread, the loop will break and print the “Foojay.io”. And the second thread will print “I love”. Thus the output is:

Shell
 
Foojay.io
I love

Case 3. The above two cases seem reasonable. However, there is a third case that we may not anticipate immediately: the first thread may be stuck and the second thread will print “I love” and that’s all. No more output. This can be difficult to reproduce, but it can happen.

Let me explain the third case!

We know the modern computer has multiple CPUs in it. Nonetheless, we cannot guarantee in which core the thread will eventually run. For example, the above two threads can run on two different CPUs, which is most likely the case.

When a CPU executes a code, it reads data from the main memory. However, modern CPUs have various caches for faster access to memory. There are three types of cache associated with each CPU. They are L1 Cache, L2 Cache, and L3 Cache.

CPU

When starting the first thread, the CPU it runs on may cache the running variable and keep it running. But, on the other hand, when the second thread runs and changes the variable on a different CPU, it won’t be visible to the first thread. Thus, the problem of the third case would occur.

We cannot tell whether this would be the case because it all depends on the operating system and having multiple CPUs in a computer. Despite this, we can prevent the CPU from caching, by using "volatile" in the variable. This will instruct the CPU not to cache the variable and, instead, it will read it from the main memory:

Java
 
public class FoojayPlayground {
   private static volatile boolean running = false;
   ...
   ...
}

Now, if we run the above program, the third case will not happen.

The above problem is called the visibility problem. There are a few similar problems that deal with the visibility issue.

We will discuss them in the following articles!

That’s it for today!

Computer Cache (computing) Data (computing) Execution (computing) intellij Memory (storage engine) operating system systems Visibility (geometry) Data Types

Published at DZone with permission of A N M Bazlur Rahman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Exciting New Features in Java 17 With Examples
  • Python Variables Declaration
  • Linked List in Data Structures and Algorithms
  • Providing Enum Consistency Between Application and Data

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!