Table of contents
1.
Introduction
2.
Processes & Threads
2.1.
Java
3.
Memory Model
4.
Frequently Asked Questions
4.1.
What is the difference between a process & a thread in Java?
4.2.
How does the Java Memory Model ensure thread safety?
4.3.
What is the purpose of the synchronized keyword in Java?
5.
Conclusion
Last Updated: Sep 19, 2025
Easy

Java Concurrency

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is a powerful programming language usually used to build various applications. As applications become more complex, they often must perform multiple tasks simultaneously. This is where concurrency comes in. Concurrency allows different parts of a program to execute independently & interact with each other when needed. It helps in utilizing system resources efficiently & improving overall performance. 

Java Concurrency

In this article, we will learn about the basics of concurrency in Java, which includes processes, threads & the memory model. We will also see how to write concurrent programs using Java's built-in concurrency tools & best practices to follow.

Processes & Threads

In Java, concurrency is achieved through processes & threads. A process is an instance of a program that is being executed. It has its own memory space & resources allocated by the operating system. On the other hand, a thread is a lightweight unit of execution within a process. A process can have multiple threads running concurrently, sharing the same memory space.

Here's a simple example to show the difference between processes & threads:

  • Java

Java

public class ProcessExample {
public static void main(String[] args) {
// Creating a new process
ProcessBuilder pb = new ProcessBuilder("notepad.exe");
try {
pb.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public class ThreadExample extends Thread {
public void run() {
System.out.println("Thread is running.");
}

public static void main(String[] args) {
// Creating & starting a new thread
ThreadExample thread = new ThreadExample();
thread.start();
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

A module you have imported isn't available at the moment. It will be available soon.


In the ProcessExample, we create a new process by launching the Notepad application using ProcessBuilder. Each instance of the notepad runs in a separate process.

In the ThreadExample, we create a new thread by extending the Thread class & overriding the run() method. The start() method is called to start the thread's execution.

Threads are easier compared to processes & are commonly used for implementing concurrency within a single application. They share the same memory space, which allows for efficient communication between threads, but also requires careful synchronization to avoid issues like race conditions & deadlocks.

Java provides several ways to create & manage threads, such as extending the Thread class, implementing the Runnable interface, or using the Executor framework for thread pooling.

Memory Model

The Java Memory Model (JMM) defines how threads interact with the memory in a concurrent program. It specifies the rules for reading & writing variables and ensures that the program behaves correctly & predictably in a multi-threaded environment.

In Java, each thread has its stack memory, which is used for storing local variables & method invocations. However, all threads share the same heap memory, which is used for storing objects & their instance variables.

The JMM introduces the concept of happens-before relationships, which define the ordering of memory operations between threads. It guarantees that if one action happens before another, the first action is visible to & ordered before the second action.

Let’s see some important aspects of the Java Memory Model:

  1. Volatile variables: When a variable is declared as volatile, it ensures that any write to the variable is immediately visible to all threads, & any read from the variable always fetches the latest written value.
     
  2. Synchronized methods & blocks: The synchronized keyword is used to achieve mutual exclusion & prevent multiple threads from accessing the same shared resource simultaneously. It ensures that only one thread can execute a synchronized block or method at a time, while other threads wait for the lock to be released.
     
  3. Final variables: Once a final variable is initialized, its value cannot be modified. This is useful for creating thread-safe immutable objects.
     
  4. Atomic operations: Java provides a set of atomic variables & operations in the java.util.concurrent.atomic package, which allows for lock-free thread-safe programming.
     

An example that demonstrates the use of volatile & synchronized:

public class SharedCounter {
    private volatile int count = 0;

    public synchronized void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}


In this example, the count variable is declared as volatile to ensure that any write to it is immediately visible to all threads. The increment() method is synchronized to prevent multiple threads from modifying the count simultaneously, avoiding race conditions.

Frequently Asked Questions

What is the difference between a process & a thread in Java?

A process is an instance of a program execution with its own memory space, while a thread is a lightweight unit of execution within a process, sharing the same memory space.

How does the Java Memory Model ensure thread safety?

The Java Memory Model defines happens-before relationships, which specify the ordering of memory operations between threads, ensuring that the program behaves correctly in a multi-threaded environment.

What is the purpose of the synchronized keyword in Java?

The synchronized keyword is used to achieve mutual exclusion & prevent multiple threads from accessing the same shared resource simultaneously, ensuring thread safety.

Conclusion

In this article, we have learned about the fundamentals of concurrency in Java. We explained the concepts of processes & threads, understood their differences & how they enable concurrent execution. We also looked into the Java Memory Model, which defines the rules for interaction between threads & memory, ensuring thread safety & predictability in concurrent programs. 

Recommended Readings:

ConcurrentHashMap in Java

Thread Safe in Java

Multithreading in Java

Understanding the Pros and Cons of Concurrency

Live masterclass