Multi-thread & Multi-process

  关于多进程和多线程,教科书上最经典的一句话是“进程是资源分配的最小单位,线程是CPU调度的最小单位”.

对于到底是使用多进程还是多线程, 要根据实际情况来判断,选择更适合的。

具体情况,可以参考下面:

1)需要频繁创建销毁的优先用多线程

  这种原则最常见的应用就是Web服务器了,来一个连接建立一个线程,断了就销毁线程,要是用进程,

创建和销毁的代价是很难承受的.

2)需要进行大量计算的优先使用多线程

  所谓大量计算,是指耗费很多CPU,切换频繁了,这种情况下线程是最合适的。这种原则最常见的是图

像处理、算法处理。

3)强相关的处理用线程,弱相关的处理用进程

  什么叫强相关、弱相关?理论上很难定义,给个简单的例子就明白了。

  一般的Server需要完成如下任务:消息收发、消息处理。“消息收发”和“消息处理”就是弱相关的任务,而

“消息处理”里面可能又分为“消息解码”、“业务处理”,这两个任务相对来说相关性就要强多了。因此“消息收发”

和“消息处理”可以分进程设计,“消息解码”、“业务处理”可以分线程设计。当然这种划分方式不是一成不变的,

也可以根据实际情况进行调整。

4)可能要扩展到多机分布的用进程,多核分布的用线程

5)都满足需求的情况下,用你最熟悉、最拿手的方式

  至于“数据共享、同步”、“编程、调试”、“可靠性”这几个维度的所谓的“复杂、简单”应该怎么取舍,其实没

有明确的选择方法。但我可以告诉你一个选择原则:如果多进程和多线程都能够满足要求,那么选择你最熟悉、

最拿手的那个。

  需要提醒的是:虽然给出了这么多的选择原则,但实际应用中基本上都是“进程+线程”的结合方式,千万不

要陷入一种非此即彼的误区。

摘录自:

浅谈多进程多线程的选择: http://www.2cto.com/kf/201007/53769.html

时间: 2024-08-09 23:54:28

Multi-thread & Multi-process的相关文章

multi thread for Java

I try to do a testing for HashTable Sychronized behavior today. As an Sychronized Object, HashTable already an Sychronized at put and get function. I wanna to know more about the iterator behaviors on multi-threading. 1 package leetcode; 2 3 import j

Multi account chang login with multi -thread

void worker_DoWork(object sender, DoWorkEventArgs e) { isBussy = true; if (Common.isChangingAccount) { rt = new ResultInfo() { code = 3, isSucc = false, msg = "now system change account" }; return; } if (isTest) { rt = new ResultInfo() { code=1,

43_2013年11月22日 线程池 Socket(Thread Lock Process 摇奖 线程池ThreadPool)

1>模拟线程池,生产者消费者问题 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Product { class Program { static void Main(string[] args) { //创建一个池子 MyConncetion[]

[CareerCup] 16.1 Thread and Process 线程和进程

16.1 What's the difference between a thread and a process? 进程Process是程序执行时的一个实例.一个进程是被分配系统资源的独立单元,每个进程在独立的地址空间上执行,如果需要使用其他进程的资源,需要使用进程间通讯,包括管道Pipes,文件Files,套接字Sockets,或者其他形式. 线程Thread存在于进程之中并分享进程的资源(包括堆空间).同一个进程中的多个线程分享同一个堆地址.这是和进程区别很大的地方,进程之间不能直接访问内

How to set an Apache Kafka multi node – multi broker cluster【z】

Set a multi node Apache ZooKeeper cluster On every node of the cluster add the following lines to the file kafka/config/zookeeper.properties server.1=zNode01:2888:3888 server.2=zNode02:2888:3888 server.3=zNode03:2888:3888 #add here more servers if yo

Thread VS Process From Stackoverflow

Preface:翻译水平渣,阅读需仔细.有错误欢迎指正. What is the difference between a thread and a process? 线程和进程之间有何区别? Processes vs Threads 进程VS线程 A process is an executing instance of an application. What does that mean? Well, for example, when you double-click the Micro

Program Thread 和 Process的不同点

Thread is for execution Kernel level thread, physical parallelism Cores Divide work amount of physical cores / CPU Load balancing Data Splitting Which will lead to data dependency coodination and message passing Bad thing for multiple cores is this i

how kernel distinguishes between thread and process

https://stackoverflow.com/questions/36213681/how-kernel-distinguishes-between-thread-and-process 原文地址:https://www.cnblogs.com/yudidi/p/12417285.html

[译]The multi Interface

The multi Interfacemulti接口 The easy interface as described in detail in this document is a synchronous interface that transfers one file at a time and doesn't return until it is done.easy接口是同步的,调用同步接口传输文件,需要等到传输完毕函数才会返回. The multi interface, on the o

Difference between Process and thread?

What are the differences between a process and a thread? How are they similar? How can 2 threads communicate? How can 2 process communicate? Both processes and threads are independent sequences of execution. The main difference is that threads (of th