In simple words, concurrency is the ability to run several programs or several parts of a program in parallel. Concurrency enables a program to achieve high performance and throughput by utilizing the untapped capabilities of the underlying operating system and machine hardware. For example, modern computers have several CPUs or several cores within one CPU, the program can utilize all cores for some part of the processing; thus completing tasks much before in time in comparison to sequential processing.
The backbone of java concurrency is threading. A thread is a lightweight process that 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. You can work with many threads within a Java application to achieve parallel processing or concurrency.
1. What makes a Java Application Concurrent?
In 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 the thread class.
Other classes you will need to build advanced applications can be found at
java.util.concurrent package added in Java 1.5.
2. Is Java Concurrency Really That Simple?
The above description gives the 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 and a clear understanding of application goals.
Concurrent applications usually have more complex designs in comparison to single-threaded applications. Code executed by multiple threads accessing shared data needs special attention. Errors arising from incorrect thread synchronization are very hard to detect, reproduce and fix. They usually show 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.
3. Java Concurrency Tutorial
Covering whole java concurrency in a single post is simply almost impossible. So, I have written below Java Concurrency Tutorials discussing one individual concept in a single post. Go through these tutorials, and let me know if you have any questions or suggestions.
3.1. Java Concurrency Basics
- Concurrency Evolution
- What is Thread Safety?
- How to Create and Start a New Thread in Java
- Set and Get the Name of Thread
- Kill a Thread
- Join a Thread
- Thread Priority
- Daemon Threads
- Object level locking and class level locking
- Compare and Swap [CAS] Algorithm
- wait(), notify() and notifyAll() methods
- Resolving IllegalMonitorStateException
- Resolving InterruptedException
3.2. Difference between
- Difference between “implements Runnable” and “extends Thread”
- Difference Between Callable and Runnable
- Difference between lock and monitor
- Difference between yield() and join()
- Difference between sleep() and wait()?
- Difference between Semaphore vs ReentrantLock
3.3. Executor Framework
- Executor Framework Tutorial
- ExecutorService shutdown(), shutdownNow() and awaitTermination()
- ExecutorService invokeAll()
- ExecutorService invokeAny()
- ScheduledThreadPoolExecutor – Task Scheduling with Executors
- ScheduledExecutorService – Running task in executor after delay
- FixedSizeThreadPoolExecutor Example
- ThreadPoolExecutor Example
- ThreadPoolExecutor + Callable + Future Example
- Throttling Task Submission using Semaphore
- BlockingQueue Example
- UncaughtExceptionHandler Example
- Waiting for Threads to Finish
- Cancel a task with ExecutorService
- Executor RejectedExecutionHandler
3.4. Advance Concurrency
- ForkJoinPool Example
- CountDownLatch Example
- Control concurrent access using semaphore
- ThreadLocal Variables
- Inter-thread communication
3.5. Concurrent Collections
3.6. Project Loom
Happy Learning !!