The Java Tutorials have been written for JDK 8.Java教程是为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.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。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. 在Java编程语言中,并发编程主要与线程有关。However, processes are also important.然而,过程也很重要。
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.单个内核的处理时间通过称为时间切片的操作系统功能在进程和线程之间共享。
Its becoming more and more common for computer systems to have multiple processors or processors with multiple execution cores. 对于计算机系统来说,拥有多个处理器或具有多个执行核心的处理器变得越来越普遍。This greatly enhances a systems 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)资源,如管道和套接字。IPC is used not just for communication between processes on the same system, but processes on different systems.IPC不仅用于同一系统上的进程之间的通信,还用于不同系统上的进程之间的通信。
Most implementations of the Java virtual machine run as a single process. Java虚拟机的大多数实现都作为单个进程运行。A Java application can create additional processes using a Java应用程序可以使用ProcessBuilder
object. ProcessBuilder
对象创建其他进程。Multiprocess applications are beyond the scope of this lesson.多进程应用程序超出了本课程的范围。
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 processs resources, including memory and open files. 线程共享进程资源,包括内存和打开的文件。This makes for efficient, but potentially problematic, communication.这有助于实现高效但可能存在问题的沟通。
Multithreaded execution is an essential feature of the Java platform. 多线程执行是Java平台的一个基本特性。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 programmers point of view, you start with just one thread, called the main thread. 但是从应用程序程序员的角度来看,您只需要从一个线程开始,称为主线程。This thread has the ability to create additional threads, as well demonstrate in the next section.该线程能够创建其他线程,并在下一节中演示。