什么是Java中的互斥和信号量?主要区别是什么? [英] What is mutex and semaphore in Java ? What is the main difference?

查看:204
本文介绍了什么是Java中的互斥和信号量?主要区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的互斥和信号量是什么?主要区别是什么?

解决方案

信号量可以计数,而互斥量只能计数到1。
$ b

假设你有一个线程运行,接受客户端连接。这个线程可以同时处理10个客户端。然后每个新客户端设置信号量直到它达到10.当信号量有10个标志时,你的线程将不会接受新的连接。



Mutex通常用于保护。假设您的10个客户端可以访问系统的多个部分。然后,您可以使用互斥锁保护系统的一部分,因此当1个客户端连接到该子系统时,没有其他人应该访问。你也可以使用信号量。互斥是相互排除信号量


What is mutex and semaphore in Java ? What is the main difference ?

解决方案

Semaphore can be counted, while mutex can only count to 1.

Suppose you have a thread running which accepts client connections. This thread can handle 10 clients simultaneously. Then each new client sets the semaphore until it reaches 10. When the Semaphore has 10 flags, then your thread won't accept new connections

Mutex are usually used for guarding stuff. Suppose your 10 clients can access multiple parts of the system. Then you can protect a part of the system with a mutex so when 1 client is connected to that sub-system, no one else should have access. You can use a Semaphore for this purpose too. A mutex is a "Mutual Exclusion Semaphore".

这篇关于什么是Java中的互斥和信号量?主要区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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