Thread versus process
Java has two units of execution—a process and a thread. A process usually represents the whole Java virtual machine (JVM), although an application can create another process using java.lang.ProcessBuilder
. But since the multi-process case is outside the scope of this book, we will focus on the second unit of execution—that is, a thread, which is similar to a process but less isolated from other threads and requires fewer resources for execution.
A process can have many threads running and at least one thread called the main thread—the one that starts the application—which we use in every example. Threads can share resources, including memory and open files, which allows for better efficiency, but this comes at a price: a higher risk of unintended mutual interference, and even blocking of the execution. That is where programming skills and an understanding of concurrency techniques are required.