Java Reference
In-Depth Information
CHAPTER 10
Concurrency
Concurrency is one of the toughest topics to handle in modern computer programming;
understanding concurrency requires the capacity of thinking abstractly, and debugging
concurrent problems is like trying to pilot an airplane by dead reckoning. Even so, with
modern releases of Java, it has become easier (and more accessible) to write bug-free
concurrent code.
Concurrency is the ability of a program to execute different (or the same) instruc-
tions at the same time. A program that is said to be concurrent has the ability to be split
up and run on multiple CPUs. By making concurrent programs, you take advantage of
today's multicore CPUs. You can even see benefit on single-core CPUs that are I/O in-
tensive.
In this chapter, we present the most common need for concurrency tasks—from run-
ning a background task to splitting a computation into work units. Throughout the
chapter, you will find the most up-to-date recipes for accomplishing concurrency in Java
8.
10-1. Starting a Background Task
Problem
You have a task that needs to run outside of your main thread.
Solution
Search WWH ::




Custom Search