监视器和信号量之间的主要区别是什么?
当前回答
A semaphore is a signaling mechanism used to coordinate between threads. Example: One thread is downloading files from the internet and another thread is analyzing the files. This is a classic producer/consumer scenario. The producer calls signal() on the semaphore when a file is downloaded. The consumer calls wait() on the same semaphore in order to be blocked until the signal indicates a file is ready. If the semaphore is already signaled when the consumer calls wait, the call does not block. Multiple threads can wait on a semaphore, but each signal will only unblock a single thread.
计数信号量跟踪信号的数量。例如,如果生产者连续发出三次信号,wait()可以被调用三次而不阻塞。二进制信号量不算数,只有“等待”和“有信号”两种状态。
互斥锁(互斥锁)是由单个线程拥有的锁。只有获得锁的线程才能再次释放它。其他试图获取锁的线程将被阻塞,直到当前所有者线程释放它。互斥锁本身并不锁任何东西——它实际上只是一个标志。但是代码可以检查互斥锁的所有权,以确保一次只有一个线程可以访问某个对象或资源。
A monitor is a higher-level construct which uses an underlying mutex lock to ensure thread-safe access to some object. Unfortunately the word "monitor" is used in a few different meanings depending on context and platform and context, but in Java for example, a monitor is a mutex lock which is implicitly associated with an object, and which can be invoked with the synchronized keyword. The synchronized keyword can be applied to a class, method or block and ensures only one thread can execute the code at a time.
其他回答
一句话回答:
监视器:一次只能控制一个线程在监视器中执行。(需要获取锁才能执行单线程)
信号量:保护共享资源的锁。(需要获取锁才能访问资源)
下面的解释实际上解释了monitor的wait()和signal()与semaphore的P和V的区别。
监控器中对条件变量的wait()和signal()操作类似于对计数信号量的P和V操作。
A wait statement can block a process's execution, while a signal statement can cause another process to be unblocked. However, there are some differences between them. When a process executes a P operation, it does not necessarily block that process because the counting semaphore may be greater than zero. In contrast, when a wait statement is executed, it always blocks the process. When a task executes a V operation on a semaphore, it either unblocks a task waiting on that semaphore or increments the semaphore counter if there is no task to unlock. On the other hand, if a process executes a signal statement when there is no other process to unblock, there is no effect on the condition variable. Another difference between semaphores and monitors is that users awaken by a V operation can resume execution without delay. Contrarily, users awaken by a signal operation are restarted only when the monitor is unlocked. In addition, a monitor solution is more structured than the one with semaphores because the data and procedures are encapsulated in a single module and that the mutual exclusion is provided automatically by the implementation.
链接:此处可进一步阅读。希望能有所帮助。
A semaphore is a signaling mechanism used to coordinate between threads. Example: One thread is downloading files from the internet and another thread is analyzing the files. This is a classic producer/consumer scenario. The producer calls signal() on the semaphore when a file is downloaded. The consumer calls wait() on the same semaphore in order to be blocked until the signal indicates a file is ready. If the semaphore is already signaled when the consumer calls wait, the call does not block. Multiple threads can wait on a semaphore, but each signal will only unblock a single thread.
计数信号量跟踪信号的数量。例如,如果生产者连续发出三次信号,wait()可以被调用三次而不阻塞。二进制信号量不算数,只有“等待”和“有信号”两种状态。
互斥锁(互斥锁)是由单个线程拥有的锁。只有获得锁的线程才能再次释放它。其他试图获取锁的线程将被阻塞,直到当前所有者线程释放它。互斥锁本身并不锁任何东西——它实际上只是一个标志。但是代码可以检查互斥锁的所有权,以确保一次只有一个线程可以访问某个对象或资源。
A monitor is a higher-level construct which uses an underlying mutex lock to ensure thread-safe access to some object. Unfortunately the word "monitor" is used in a few different meanings depending on context and platform and context, but in Java for example, a monitor is a mutex lock which is implicitly associated with an object, and which can be invoked with the synchronized keyword. The synchronized keyword can be applied to a class, method or block and ensures only one thread can execute the code at a time.
Monitor是设计用于从多个线程访问的对象。监视对象的成员函数或方法将强制互斥,因此在给定时间只有一个线程可以对对象执行任何操作。如果一个线程正在执行该对象的成员函数,那么任何试图调用该对象的成员函数的其他线程都必须等待,直到第一个线程完成。
信号量是较低级别的对象。您可以使用信号量来实现监视器。信号量本质上就是一个计数器。当计数器为正数时,如果线程试图获取信号量,则允许获取信号量,计数器则递减。当一个线程完成时,它释放信号量,并增加计数器。
如果当一个线程试图获取信号量时计数器已经为零,那么它必须等待另一个线程释放信号量。如果一个线程释放一个信号量时有多个线程在等待,那么其中一个线程会得到它。释放信号量的线程不一定是获取信号量的线程。
显示器就像一个公共厕所。每次只能有一个人进入。他们锁上门防止其他人进来,做他们的事情,然后在他们离开的时候打开门。
信号量就像一个自行车租赁场所。他们有一定数量的自行车。如果你想租一辆自行车,他们有一辆免费的,那么你可以骑,否则你必须等待。当有人归还自行车时,其他人就可以拿走它。如果你有一辆自行车,那么你可以把它交给别人归还——自行车租赁公司不在乎谁来归还,只要他们能把自行车拿回来就行。
信号量允许多个线程(最多设置一个数目)访问一个共享对象。监视器允许对共享对象的互斥访问。
监控
信号量
推荐文章
- ExecutorService,如何等待所有任务完成
- 在python中创建线程
- 处理来自Java ExecutorService任务的异常
- c#事件和线程安全
- Thread start()和Runnable run()有什么区别
- 如何在Python中获取线程id ?
- 一个Java虚拟机可以支持多少线程?
- 到底什么是std::atomic?
- RxJava调度器用例
- 获取当前在Java中运行的所有线程的列表
- c#静态构造函数线程安全吗?
- CompletableFuture, Future和RxJava的Observable之间的区别
- 信号量和监视器——有什么不同?
- 同时运行多个asynctask——不可能?
- java.lang.Thread.interrupt()做什么?