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

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。这个线程具有创建新线程的能力。Java提供Thread class。

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

executor属于high level concurrency objects see:

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

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值