Java 中的互斥量和信号量是什么?主要区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/771347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is mutex and semaphore in Java ? What is the main difference?
提问by Tony
What is mutex and semaphore in Java ? What is the main difference ?
Java 中的互斥量和信号量是什么?主要区别是什么?
采纳答案by Eric
Semaphore can be counted, while mutex can only count to 1.
信号量可以计数,而互斥量只能计数到 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
假设您有一个接受客户端连接的线程正在运行。该线程可以同时处理 10 个客户端。然后每个新客户端设置信号量直到达到 10。当信号量有 10 个标志时,您的线程将不会接受新连接
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".
互斥通常用于保护东西。假设您的 10 个客户端可以访问系统的多个部分。然后,您可以使用互斥锁保护系统的一部分,因此当 1 个客户端连接到该子系统时,其他人不应具有访问权限。您也可以为此目的使用信号量。互斥体是“互斥信号量”。
回答by Brian Rasmussen
A semaphore is a counting synchronization mechanism, a mutex isn't.
信号量是一种计数同步机制,互斥量不是。
回答by Jason Coco
A mutex is used for serial access to a resource while a semaphore limits access to a resource up to a set number. You can think of a mutex as a semaphore with an access count of 1. Whatever you set your semaphore count to, that may threads can access the resource before the resource is blocked.
互斥体用于对资源的串行访问,而信号量将访问资源的访问限制为设定的数量。您可以将互斥体视为访问计数为 1 的信号量。无论您将信号量计数设置为什么,线程都可以在资源被阻塞之前访问该资源。
回答by aJ.
Mutex is basically mutual exclusion. Only one thread can acquire the resource at once. When one thread acquires the resource, no other thread is allowed to acquire the resource until the thread owning the resource releases. All threads waiting for acquiring resource would be blocked.
Mutex 基本上是互斥的。一次只有一个线程可以获取资源。当一个线程获取资源时,在拥有该资源的线程释放之前,不允许其他线程获取该资源。所有等待获取资源的线程都会被阻塞。
Semaphore is used to control the number of threads executing. There will be fixed set of resources. The resource count will gets decremented every time when a thread owns the same. When the semaphore count reaches 0 then no other threads are allowed to acquire the resource. The threads get blocked till other threads owning resource releases.
信号量用于控制执行的线程数。将有固定的资源集。每次线程拥有相同资源时,资源计数都会递减。当信号量计数达到 0 时,不允许其他线程获取资源。线程被阻塞,直到拥有资源的其他线程释放。
In short, the main difference is how many threads are allowed to acquire the resource at once ?
简而言之,主要区别在于一次允许多少个线程获取资源?
- Mutex --its ONE.
- Semaphore -- its DEFINED_COUNT, ( as many as semaphore count)
- 互斥体——它的一个。
- 信号量——它的 DEFINED_COUNT,(与信号量一样多)
回答by Sean
A mutex is often known as a binary semaphore. Whilst a semaphore can be created with any non-zero count a mutex is conceptually a semeaphore with an upper count of 1.
互斥体通常被称为二进制信号量。虽然可以使用任何非零计数创建信号量,但互斥量在概念上是计数上限为 1 的信号量。
回答by feabhas
Unfortunately everyone has missed the most important difference between the semaphore and the mutex; the concept of "ownership".
不幸的是,每个人都忽略了信号量和互斥量之间最重要的区别;“所有权”的概念。
Semaphores have no notion of ownership, this means that any thread can release a semaphore (this can lead to many problems in itself but can help with "death detection"). Whereas a mutex does have the concept of ownership (i.e. you can only release a mutex you have acquired).
Ownership is incredibly important for safe programming of concurrent systems. I would always recommend using mutex in preference to a semaphore (but there are performance implications).
信号量没有所有权的概念,这意味着任何线程都可以释放信号量(这本身会导致许多问题,但有助于“死亡检测”)。而互斥锁确实具有所有权的概念(即您只能释放您获得的互斥锁)。
所有权对于并发系统的安全编程非常重要。我总是建议优先使用互斥量而不是信号量(但有性能影响)。
Mutexes also may support priority inheritance (which can help with the priority inversion problem) and recursion (eliminating one type of deadlock).
互斥体还可以支持优先级继承(可以帮助解决优先级反转问题)和递归(消除一种死锁)。
It should also be pointed out that there are "binary" semaphores and "counting/general" semaphores. Java's semaphore is a counting semaphore and thus allows it to be initialized with a value greater than one (whereas, as pointed out, a mutex can only a conceptual count of one). The usefulness of this has been pointed out in other posts.
还应该指出的是,有“二进制”信号量和“计数/通用”信号量。Java 的信号量是一个计数信号量,因此允许使用大于 1 的值对其进行初始化(而正如所指出的,互斥量只能在概念上计数为 1)。其他帖子中已经指出了这一点的用处。
So to summarize, unless you have multiple resources to manage, I would always recommend the mutex over the semaphore.
总而言之,除非您有多个资源需要管理,否则我总是建议使用互斥量而不是信号量。
回答by Air
This question has relevant answers and link to official Java guidance: Is there a Mutex in Java?
这个问题有相关的答案和官方 Java 指南的链接:Java 中有互斥锁吗?
回答by Marwen Trabelsi
You compare the incomparable, technically there is no difference between a Semaphore and mutex it doesn't make sense. Mutex is just a significant name like any name in your application logic, it means that you initialize a semaphore at "1", it's used generally to protect a resource or a protected variable to ensure the mutual exclusion.
你比较无可比拟,从技术上讲,信号量和互斥量之间没有区别,这没有意义。Mutex 只是一个重要的名称,就像应用程序逻辑中的任何名称一样,这意味着您将信号量初始化为“1”,它通常用于保护资源或受保护的变量以确保互斥。
回答by dim8
Mutex is binary semaphore. It must be initialized with 1, so that the First Come First Serve principle is met. This brings us to the other special property of each mutex: the one who did down, must be the one who does up. Ergo we have obtained mutual exclusion over some resource.
互斥是二进制信号量。它必须初始化为 1,以便满足先来先服务原则。这给我们带来了每个互斥锁的另一个特殊属性:执行down 的必须是执行up 的那个。因此,我们在某些资源上获得了互斥。
Now you could see that a mutex is a special case of general semaphore.
现在您可以看到互斥锁是一般信号量的特例。
回答by Amarildo
The object of synchronization Semaphoreimplements a classical traffic light. A traffic light controls access to a resource shared by a counter. If the counter is greater than zero, access is granted; If it is zero, access is denied. The counter counts the permissions that allow access to the shared resource. Then, to access the resource, a thread must receive permission from the traffic light. In general, to use a traffic light, the thread that wants to access the shared resource tries to acquire a permit. If the traffic light count is greater than zero, the thread acquires a permit, and the traffic light count is decremented. Otherwise the thread is locked until it can get a permission. When the thread no longer needs to access the shared resource, it releases the permission, so the traffic light count is increased. If there is another thread waiting for a permit, it acquires a permit at that time. The Semaphore class of Java implements this mechanism.
同步信号量的对象实现了一个经典的交通灯。交通灯控制对计数器共享资源的访问。如果计数器大于零,则授予访问权限;如果为零,则拒绝访问。计数器计算允许访问共享资源的权限。然后,要访问资源,线程必须从交通灯获得许可。通常,要使用红绿灯,想要访问共享资源的线程会尝试获取许可。如果交通灯计数大于零,线程获得许可,并且交通灯计数递减。否则线程将被锁定,直到它获得许可。当线程不再需要访问共享资源时,释放权限,所以红绿灯计数增加。如果有另一个线程在等待许可,它当时获得了许可证。Java 的 Semaphore 类实现了这种机制。
Semaphore has two builders:
信号量有两个构建器:
Semaphore(int num)
Semaphore(int num, boolean come)
numspecifies the initial count of the permit. Then num specifies the number of threads that can access a shared resource at a given time. If num is one, it can access the resource one thread at a time. By setting comeas true, you can guarantee that the threads you are waiting for are granted permission in the order they requested.
num指定许可证的初始计数。然后 num 指定在给定时间可以访问共享资源的线程数。如果 num 为 1,它可以一次一个线程访问资源。通过设置comeas true,您可以保证您正在等待的线程按照它们请求的顺序被授予权限。