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

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

问题描述

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

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

推荐答案

信号量可以计数,而互斥量只能计数到1.

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

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

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

互斥锁通常用于保护东西.假设您的 10 个客户端可以访问系统的多个部分.然后,您可以使用互斥锁保护系统的一部分,因此当 1 个客户端连接到该子系统时,其他人不应具有访问权限.您也可以为此目的使用信号量.互斥锁是一个互斥信号量".

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天全站免登陆