The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
Java 教程是为 JDK 8 编写的。本页中描述的示例和做法不利用更高版本中引入的改进,并且可能使用不再可用的技术。
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
有关 Java SE 9 和后续发行版中更新的语言功能的摘要,请参阅 Java 语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
有关所有 JDK 版本的新功能、增强功能以及已删除或弃用选项的信息,请参阅 JDK 发行说明。
In concurrent programming, there are two basic units of execution: processes and threads. In the Java programming language, concurrent programming is mostly concerned with threads. However, processes are also important.
在并发编程中,有两个基本的执行单元:进程和线程。在 Java 编程语言中,并发编程主要关注线程。但是,流程也很重要。
A computer system normally has many active processes and threads. This is true even in systems that only have a single execution core, and thus only have one thread actually executing at any given moment. Processing time for a single core is shared among processes and threads through an OS feature called time slicing.
计算机系统通常具有许多活动进程和线程。即使在只有一个执行核心的系统中也是如此,因此在任何给定时刻只有一个线程实际执行。单个内核的处理时间通过称为时间切片的操作系统功能在进程和线程之间共享。
It's becoming more and more common for computer systems to have multiple processors or processors with multiple execution cores. This greatly enhances a system's capacity for concurrent execution of processes and threads — but concurrency is possible even on simple systems, without multiple processors or execution cores.
计算机系统具有多个处理器或具有多个执行内核的处理器变得越来越普遍。这大大增强了系统并发执行进程和线程的能力,但即使在没有多个处理器或执行核心的简单系统上也可以实现并发。
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.
进程具有独立的执行环境。进程通常具有一组完整的专用基本运行时资源;特别是,每个进程都有自己的内存空间。
Processes are often seen as synonymous with programs or applications. However, what the user sees as a single application may in fact be a set of cooperating processes. To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources, such as pipes and sockets. IPC is used not just for communication between processes on the same system, but processes on different systems.
进程通常被视为程序或应用程序的同义词。但是,用户所看到的单个应用程序实际上可能是一组协作进程。为了促进进程之间的通信,大多数操作系统都支持进程间通信 (IPC) 资源,例如管道和套接字。IPC不仅用于同一系统上的进程之间的通信,还用于不同系统上的进程之间的通信。
Most implementations of the Java virtual machine run as a single process. A Java application can create additional processes using a
ProcessBuilder
object. Multiprocess applications are beyond the scope of this lesson.
Java 虚拟机的大多数实现都作为单个进程运行。Java 应用程序可以使用对象 ProcessBuilder
创建其他进程。多进程应用程序超出了本课程的范围。
Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.
线程有时称为轻量级进程。进程和线程都提供执行环境,但创建新线程所需的资源比创建新进程少。
Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication.
线程存在于一个进程中——每个进程至少有一个线程。线程共享进程的资源,包括内存和打开的文件。这样可以高效地进行沟通,但可能会带来问题。
Multithreaded execution is an essential feature of the Java platform. Every application has at least one thread — or several, if you count "system" threads that do things like memory management and signal handling. But from the application programmer's point of view, you start with just one thread, called the main thread. This thread has the ability to create additional threads, as we'll demonstrate in the next section.
多线程执行是 Java 平台的基本特性。每个应用程序都至少有一个线程,如果算上执行内存管理和信号处理等操作的“系统”线程,则为多个线程。但从应用程序程序员的角度来看,你只从一个线程开始,称为主线程。此线程能够创建其他线程,我们将在下一节中演示。