Java Concurrency Tutorial

In simple words, concurrency is the ability to run several programs or several parts of a program in parallel. Concurrency enable a program to achieve high performance and throughput by utilizing the untapped capabilities of underlying operating system and machine hardware. e.g. modern computers has several CPU’s or several cores within one CPU, program can utilize all cores for some part of processing; thus completing task much before in time in comparison to sequential processing.

The backbone of java concurrency are threads. A thread is a lightweight process which has its own call stack, but can access shared data of other threads in the same process. A Java application runs by default in one process. Within a Java application you can work with many threads to achieve parallel processing or concurrency.

What makes java application concurrent?

The very first class, you will need to make a java class concurrent, is java.lang.Thread class. This class is the basis of all concurrency concepts in java. Then you have java.lang.Runnable interface to abstract the thread behavior out of thread class.

Other classes you will need to build advance applications can be found at java.util.concurrent package added in Java 1.5.

Read More: Java concurrency evolution

Is java concurrency really that simple?

Above description gives impression that concurrency is indeed a good concept, and is very easy to implement. Well, it is not. It requires a good amount of understanding of the basic concepts – as well as – clear understanding of application goals.

Concurrent applications usually have more complex design in comparison to single threaded application. Code executed by multiple threads accessing shared data need special attention. Errors arising from incorrect thread synchronization are very hard to detect, reproduce and fix. They usually shows up in higher environments like production, and replicating the error is sometimes not possible in lower environments.

Apart from complex defects, concurrency requires more resources to run the application. So make sure, you have sufficient resources in your kitty.

Java Concurrency Tutorial

Covering whole java concurrency in single post is simply almost impossible. So, I have written below Java Concurrency Tutorials discussing one individual concept in single post. Go through these tutorials, and let me know if you have any questions or suggestions.

Java Concurrency Basics

Concurrency Evolution
What is Thread Safety?
Object level locking and class level locking
Compare and Swap [CAS] Algorithm
wait(), notify() and notifyAll() methods

Difference between

Difference between “implements Runnable” and “extends Thread”
Difference between lock and monitor
Difference between yield() and join()
Difference between sleep() and wait()?

Executor Framework

Executor framework tutorial
ScheduledThreadPoolExecutor Example
FixedSizeThreadPoolExecutor Example
ThreadPoolExecutor Example
ThreadPoolExecutor + Callable + Future Example
Throttling task submission rate using ThreadPoolExecutor and Semaphore
BlockingQueue Example
UncaughtExceptionHandler Example

Advance Concurrency

ForkJoinPool Example
CountDownLatch Example
Control concurrent access using semaphore
BinarySemaphore
java.util.concurrent.locks.Lock
java.util.concurrent.ThreadFactory
ThreadLocal Variables
Inter-thread communication

Concurrent Collections

ConcurrentHashMap Example
ConcurrentLinkedDeque Example

Miscellaneous

Creating and resolving Deadlock

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.