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…
In Java, a synchronized block of code can only be executed by one thread at a time. Also, java supports multiple threads to be executed concurrently. This may cause two or more threads to access the same fields or objects at the same time. Synchronization is the process which keeps all concurrent threads in execution to…
Defining thread safety is surprisingly tricky. A quick Google search turns up numerous “definitions” like these: Thread-safe code is code that will work even if many Threads are executing it simultaneously. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same…
1. Future cancel() API The Future.cancel() method takes one argument of typeboolean boolean cancel(boolean mayInterruptIfRunning); Depending on the value of mayInterruptIfRunning and the status of the task submitted to the executor, the behavior of this method is different: If the task has been completed or has been canceled earlier, or it can’t be cancelled due to any other reason, the…
Executor RejectedExecutionHandler 1. When tasks get rejected Remember, when we finish the execution of an executor, we use the shutdown() method. The executor waits for the completion of tasks that are either running or waiting for their execution. Then, it shuts down the executor. If we send a task to an executor between invoking the shutdown() method and the end…