UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

44 lines (43 loc) 4.05 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Chapter 9. Threads</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="chapter" title="Chapter 9. Threads"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-9"/>Chapter 9. Threads</h1></div></div></div><p>We take for granted that modern computer systems can manage many applications and operating system (OS) tasks running concurrently and make it appear that all the software is running simultaneously. While most systems today do have multiple processors and some processors can perform tricks to gain some degree of parallelism, for the most part a processor can only really handle one job at at time and what we are seeing is sleight of hand in the operating system, which juggles applications and turns its attention from one to the next so quickly that they appear to run at once.</p><p>In the old days, the unit of concurrency for such systems was the application or <a id="I_indexterm9_id713960" class="indexterm"/><span class="emphasis"><em>process</em></span>. To the OS, a process was more or less a black box that decided what to do on its own. If an application required greater concurrency, it could get it only by running multiple processes and communicating between them, but this was a heavyweight approach and not very elegant. Later, the concept of <a id="I_indexterm9_id713971" class="indexterm"/><span class="emphasis"><em>threads</em></span> was introduced. Threads provide fine-grained concurrency within a process under the application’s own control. Threads have existed for a long time, but have historically been tricky to use. In Java, support for threading is built into the language, making it easier to work with threads. The Java concurrency utilities address common patterns and practices in multithreaded applications and raise them to the level of tangible Java APIs. Collectively, this means that Java is a language that supports threading both natively and at a high level. It also means that Java’s APIs take full advantage of threading, so it’s important that you gain some degree of familiarity with these concepts early in your exploration of Java. Not all developers will need to write applications that explicitly use threads or concurrency, but most will use some feature that is impacted by them.</p><p>Threads are integral to the design of many Java APIs, especially those involved in client-side applications, graphics, and sound. For example, when we look at GUI programming later in this book, you’ll see that a component’s <a id="I_indexterm9_id714005" class="indexterm"/><code class="literal">paint()</code> method isn’t called directly by the application but rather by a separate drawing thread within the Java runtime system. At any given time, many such background threads may be performing activities in parallel with your application. On the server side, writing code that does explicit thread handling is less common and actively discouraged in the context of application servers and web applications. In those scenarios, the server environment should control the allocation of time. However, Java threads are there, servicing every request and running your application components. It’s important to understand how your code fits into that environment.</p><p>In this chapter, we’ll talk about writing applications that create and use their own threads explicitly. We’ll talk about the low-level thread support built into the Java language first and then discuss the <a id="I_indexterm9_id714042" class="indexterm"/><code class="literal">java.util.concurrent</code> thread utilities package in detail at the end of this chapter.</p></div></body></html>