当我有线程时,是否存在非阻塞接收的用例? [英] Is there an use case for non-blocking receive when I have threads?

查看:77
本文介绍了当我有线程时,是否存在非阻塞接收的用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道非阻塞接收在消息传递中使用不多,但是一些直觉告诉我,这是必需的.以GUI事件驱动的应用程序为例,您需要某种方式以非阻塞方式等待消息,以便您的程序可以执行一些计算.解决此问题的方法之一是使用带有消息队列的特殊线程.是否有一些用例,即使您有线程,您真的需要无阻塞接收吗?

I know non-blocking receive is not used as much in message passing, but still some intuition tells me, it is needed. Take for example GUI event driven applications, you need some way to wait for a message in a non-blocking way, so your program can execute some computations. One of the ways to solve this is to have a special thread with message queue. Is there some use case, where you would really need non-blocking receive even if you have threads?

推荐答案

线程的工作方式与非阻塞异步操作不同,尽管您通常可以通过使线程执行同步操作来达到相同的效果.但是,最后,它归结为如何更有效地处理事务.

Threads work differently than non-blocking asynchronous operations, although you can usually achieve the same effect by having threads that does synchronous operations. However, in the end, it boils down on how to handle doing things more efficiently.

线程是有限的资源,应用于处理长时间运行的活动操作.如果您的工作不是很活跃,但是需要空闲一段时间等待结果(请考虑通过网络进行一些I/O操作,例如调用Web服务或数据库服务器),那么最好使用提供的服务.异步替代方案,而不是通过将同步调用放在另一个线程上来不必要地浪费线程.

Threads are limited resources, and should be used to process long running, active operations. If you have something that is not really active doing things, but need to wait idly for some time for the result (think some I/O operation over the network like calling web services or database servers), then it is better to use the provided asynchronous alternative for it instead of wasting threads unnecessarily by putting the synchronous call on another thread.

您可以很好地阅读此问题,

You can have a good read on this issue here for more understanding.

这篇关于当我有线程时,是否存在非阻塞接收的用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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