The source code examples from this up-to-date tutorial are developed using JDK 8 or later (Lambda expressions, functional interfaces etc) and well tested on our local development environment.
In this tutorial, we will learn low-level APIs that have been part of the Java platform from the very beginning. These APIs are adequate for very basic tasks. In Java Concurrency Tutorial, we will learn high-level concurrency features introduced with version 5.0 of the Java platform. Java provides multithreading support with the Thread class and an application can create multiple threads executing concurrently.
There is a separate Java Concurrency Tutorial for high-level concurrency features Executor framework introduced with version 5.0 of the Java platform.
Another important advantage of the Executor framework is the Callable interface. It's similar to the Runnable interface, but offers two improvements, which are as follows:
1. The main method of this interface, named call(), may return a result.
2. When you send a Callable object to an executor, you get an object that implements the Future interface. You can use this object to control the status and the result of the Callable object.
- ExecutorService Interface in Java
- ScheduledExecutorService Interface in Java
- Future Interface in Java
- Java Callable and Future Tutorial
- Executors newSingleThreadExecutor Method Example
- Executors newFixedThreadPool Method Example
- Executors newCachedThreadPool Method Example
- Executors newScheduledThreadPool Method Example
- At a low level, we can create a thread in two ways, either by implementing Runnable or by subclassing Thread and overriding the run() method.
- At a high-level, we use Executors, which use thread pools, which in turn use worker threads.
- One type of thread pool is the fixed thread pool, which has a fixed number of threads running. We can also use single-thread pools.
- ExecutorService has methods to execute thread pools that either take a Runnable or Callable task. A Callable returns a result and throws a checked exception.
- The submit() method returns a Future object that represents the result of the task (if the task is a Runnable, null is returned).
- An executor has to be shutdown to close the pool thread with either shutdown() (gracefully) or shutdownNow() (forcefully).
- A deadlock situation occurs when two or more threads are blocked forever, waiting for each other to acquire/release some resource.
- Starvation happens when a thread is constantly waiting for a lock, never able to take it because other threads with higher priority are continually acquiring it.
- A livelock is like a deadlock in the sense that two (or more) threads are blocking each other, but in a livelock, each thread tries to resolve the problem on its own (live) instead of just waiting (dead).
- A race condition is a situation where two threads compete to access or modify the same resource at the same time in a way that causes unexpected results.
Check out top beginners to expert up-to-date Core Java Tutorial (100+ Articles)