(本部分原文链接,译文链接,译者:bjsuo,校对:郑旭东)
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),如【管道】pipes 和【插座】sockets。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平台的一个基本特征。每个应用都至少有一个线程 – 或几个,如果算上“系统”线程的话,比如内存管理和信号处理等。但是从程序员的观点来看,启动的只有一个线程,叫主线程。这个线程有能力创建额外的线程,我们将在下一节演示。