什么时候应该使用多线程?如果不同的线程执行相互独立的任务,多线程会有益吗? [英] When should you use multithreading? And would multi threading be beneficial if the different threads execute mutually independent tasks?

查看:166
本文介绍了什么时候应该使用多线程?如果不同的线程执行相互独立的任务,多线程会有益吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在昨晚被拒绝的采访中无法回答的两个问题。

This were the only two questions I couldn't answer in the interview I got rejected from last night.

推荐答案

Q :你什么时候应该使用多线程?

Q: When should you use multithreading?

答:你的问题非常广泛。很少有非平凡的系统能够简单,快速,可靠地实现功能。只有一个线程。例如:[挑选一个目标公司销售的典型系统,并选择其功能的几个方面,更好的线程 - 重型CPU,通信,多用户 - 只需选择一些可能的&解释]。

A: "Your question is very broad. There are few non-trivial systems where the functionality can be met simply, quickly and reliably with only one thread. For example: [pick out a typical system that the target company sells and pick out a couple aspects of its function that would be better threaded off - heavy CPU, comms, multi-user - just pick out something likely & explain].

问:如果不同的线程执行相互独立的任务,多线程会有益吗?

Q: Would multithreading be beneficial if the different threads execute mutually independent tasks?

A: 取决于'执行任务'的意思。如果线程以并发方式处理相互独立的数据,多线程肯定会是有益的 - 它减少了对锁的要求,并且以锁的数量超线性方式增加了死锁的概率。 OTOH,执行相同代码的线程没有问题,这是安全且非常常见的。

A: "Depends on what you mean by 'executing tasks'. Multithreading would surely be beneficial if the threads process mutually independent data in a concurrent fashion - it reduces requirements for locks and probabilty of deadlocks increases in a super-linear fashion with the number of locks. OTOH, there is no issue with threads executing the same code, this is safe and very common."

Rgds,
Martin

Rgds, Martin

这篇关于什么时候应该使用多线程?如果不同的线程执行相互独立的任务,多线程会有益吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆