这是用户在 2024-7-18 10:32 为 https://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Documentation 文档

The Java™ Tutorials Java™ 教程
Processes and Threads 进程和线程
Trail: Essential Java Classes
Lesson: Concurrency

Processes and Threads 进程和线程

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.
计算机系统具有多个处理器或具有多个执行内核的处理器变得越来越普遍。这大大增强了系统并发执行进程和线程的能力,但即使在没有多个处理器或执行核心的简单系统上也可以实现并发。

Processes 过程

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 线程

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 平台的基本特性。每个应用程序都至少有一个线程,如果算上执行内存管理和信号处理等操作的“系统”线程,则为多个线程。但从应用程序程序员的角度来看,你只从一个线程开始,称为主线程。此线程能够创建其他线程,我们将在下一节中演示。


Previous page: Concurrency
Next page: Thread Objects