程序 VS 进程 VS 线程 VS 任务


Program vs. Process vs. Thread vs. Task

These are four very similar terms yet very different. Lets start understanding the difference between them.

As everybody says its better to visualize than just blabber. So I have put all my thoughts about these 4 terms in the form of a diagram as shown below.

blog4

Now here comes the textual part:

A program in Simple terms can be described as any executable file. Basically it contains certain set of instructions written with the intent of carrying out a specific operation. It resides in Memory & is a passive entity which doesn’t go away when system reboots.




Any running instance of a program is called as process or it can also be described as a program under execution. 1 program can have N processes. Process resides in main memory & hence disappears whenever machine reboots. Multiple processes can be run in parallel on a multiprocessor system.



A Thread is commonly described as a lightweight process. 1 process can have N threads. All threads which are associated with a common process share same memory as of process.The essential difference between a thread and a process is the work that each one is used to accomplish. Threads are being used for small & compact tasks, whereas processes are being used for more heavy tasks.




One major difference between a thread and a process is that threads within the same process consume the same address space, whereas different processes do not. This allows threads to read from and write to the common shared and data structures and variables, and also increases ease of communication between threads. Communication between two or more processes – also known as Inter-Process Communication i.e. IPC – is quite difficult and uses intensive resources.








Tasks are very much similar to threads, the difference is that they generally do not interact directly with OS. Like a Thread Pool, a task does not create its own OS thread. A task may or may not have more than one thread internally.

If you want to know when to use Task and when to use Thread: Task is simpler to use and more efficient that creating your own Threads. But sometimes, you need more control than what is offered by Task. In those cases, it makes more sense to use Thread directly.

The bottom line is that Task is almost always the best option; it provides a much more powerful API and avoids wasting OS threads.The only reasons to explicitly create your own Threads in modern code are setting per-thread options, or maintaining a persistent thread that needs to maintain its own identity.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值