目录
并发
进程和线程
1. 进程和线程
2. 进程
3.线程
总结
并发
并发(Concurrency)
Computer users take it for granted that their systems can do more than one thing at a time. They assume that they can continue to work in a word processor, while other applications download files, manage the print queue, and stream audio. Even a single application is often expected to do more than one thing at a time.计算机用户认为他们的系统一次可以执行多项操作是理所当然的。他们假设自己可以继续在文字处理器中工作,而其他应用程序则可以下载文件,管理打印队列和流音频。通常甚至一个应用程序一次都可以完成多项任务The Java platform is designed from the ground up to support concurrent programming, with basic concurrency support in the Java programming language and the Java class libraries. Since version 5.0, the Java platform has also included high-level concurrency APIs. This lesson introduces the platform's basic concurrency support and summarizes some of the high-level APIs in the java.util.concurrent packages.Java平是从一开始就设计为支持并发编程,并在 Java 编程语言和 Java 类库中提供基本的并发支持。从5.0版开始, Java 平台还包含高级并发 API 。本课介绍了平台的基本并发支持,并总结了java.util.concurrent包中的一些高级 API 。
在并发的概念中还包含并行在其中。
进程和线程
1. 进程和线程
进程和线程
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.计算机系统通常具有许多活动的进程和线程。 即使在只有一个执行核心也是如此,因此在任何给定时刻只有一个线程实际执行。单个内核的处理时间通过OS被称作时间分片的功能在进程和线程之间共享。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.计算机系统具有多个处理器或具有多个执行核心的处理器正变得越来越普遍。 这极大地增强了系统同时执行进程和线程的能力-但即使在没有多个处理器或执行核心的简单系统上,并发也是可能的。
2. 进程
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.Java虚拟机的大多数实现都是作为单个进程运行的
3.线程
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 平台的基本功能。 每个应用程序都至少有一个线程或者几个,如果算上 “ 系统 ” 线程做的事情,如内存管理和信号处理。 但是从应用程序程序员的角度来看,您仅从一个线程(称为主线程)开始。 该线程具有创建其他线程的能力,我们将在下一部分中进行演示。
总结
- 进程拥有自己独立的内存空间,换言之就是计算机分配内存的单位是进程。线程是在进程中,可以共享进程的资源比如内存。
- 单个执行核心是通过操作系统的时间分片功能来进行进程和线程之间的处理时间的分配