多线程(一) 进程和线程的基本概念

来源:互联网 发布:网络保密整改报告 编辑:程序博客网 时间:2024/06/06 10:58

http://download.oracle.com/javase/tutorial/essential/concurrency/runthread.html

 

进程和线程的基本概念:

 

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.

 

什么是process(进程)?

通常一个程序就是一个process(进程),但是实际上一个程序也可以有多个进程。

A process has a self-contained execution environment. A processgenerally has a complete, private set of basic run-time resources; inparticular, each process has its own memory space.

 

为了提供进程之间的通信, 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.

 

Most implementations of the Java virtual machine run as a single process. A Java application can create additional processes using aProcessBuilder object.

 

 

什么是thread(线程)?

Threads are sometimes called lightweight processes. Both processesand threads provide an execution environment, but creating a new threadrequires 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.

 

从程序员的角度看,我们从一个线程开始,names main thread。这个线程具有创建新线程的能力。

 

Each thread is associated with an instance of the class Thread. There are two basic strategies for using Thread objects to create a concurrent application.

  • 想要直接控制线程的创建和管理,就在程序需要执行异步任务时,简单的实例化一个thread对象。
  • 要是不想自己做线程管理,想把线程管理从程序里抽出来,就把程序任务传递给 an executor. executor就会执行相关的管理。

executor属于high level concurrency objects see:

http://download.oracle.com/javase/tutorial/essential/concurrency/highlevel.html

 

这篇文章仅涉及第一种方法。