The Java™ Tutorials — Concurrency :Processes and Threads 进程和线程

The Java™ Tutorials — Concurrency :Processes and Threads 进程和线程

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html

关键点

  • 理解进线程的区别

全文翻译

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虚拟机的实现都将JVM安排在一个独立的进程中运行。一个Java程序可以利用ProcessBuilder对象创建额外的进程。多进程应用并不在本课时的讨论范围内。

Thread 线程

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平台的一个基本特性。每个应用都至少有一个线程——但是如果你把负责诸如内存管理、信号处理的“系统”线程也算上的话,那就是有多个。但是从开发者的角度看,你是从一个线程开始的,即主线程。主线程具有创建其他线程的能力,我们会在下一节加以讨论此点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值