什么是 Java Semaphore 和 Mutex – Java Concurrency MultiThread 举例说明
已发表: 2015-03-12Java并发是一个非常广泛的话题。 有数百个教程和示例可供使用。 前段时间我写过一些关于在 Java 中并发运行多个线程和不同类型的同步块的教程。
在本教程中,我们将介绍:
- 互斥量的解释
- 信号量的解释
- 两个有细节的例子
让我们开始吧
在阅读以下说明时Let's keep this in mind
:
- 以 Shopper 和 Customer 为例
- 购物者正在借笔记本电脑
- 客户可以来使用笔记本电脑——客户需要钥匙才能使用笔记本电脑
- 使用后 – 客户可以将笔记本电脑退还给购物者
什么是互斥锁(只有 1 个线程):
购物者有笔记本电脑的钥匙。 一位客户当时可以拥有钥匙——借用一台笔记本电脑。 当任务完成时,Shopper 将钥匙交给(释放)队列中的下一位顾客。
Official Definition
:
“互斥锁通常用于序列化对cannot be executed concurrently
的可re-entrant code
部分的访问。 互斥对象只允许一个线程进入受控部分,迫使试图访问该部分的其他线程等待,直到第一个线程退出该部分。”
换句话说: Mutex = Mutually Exclusive Semaphore
什么是信号量(N个指定线程):
假设现在 Shopper 有 3 台相同的笔记本电脑和 3 个相同的钥匙。 信号量是free identical Laptop keys
的数量。 信号量计数——键的数量——在开始时设置为 3(所有三台笔记本电脑都是空闲的),然后随着客户进来,计数值递减。如果所有笔记本电脑都在使用,即没有空闲键可供使用笔记本电脑,信号量计数为 0。现在,当任何客户返回笔记本电脑时,信号量增加到 1(一个空闲密钥),并提供给队列中的下一个客户。
Official Definition
:“信号量将共享资源的同时用户数限制为最大数量。 线程可以请求对资源的访问(减少信号量),并且可以发出信号表明它们已经完成了对资源的使用(增加信号量)。”
另一个必须阅读:单例线程安全实例的延迟创建
示例 1:(解释如下)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
package crunchify . com . tutorial ; import java . util . LinkedList ; import java . util . concurrent . Semaphore ; /** * @author Crunchify.com * */ public class CrunchifySemaphoreMutexTutorial { static Object crunchifyLock = new Object ( ) ; static LinkedList <String> crunchifyList = new LinkedList <String> ( ) ; // Semaphore maintains a set of permits. // Each acquire blocks if necessary until a permit is available, and then takes it. // Each release adds a permit, potentially releasing a blocking acquirer. static Semaphore semaphore = new Semaphore ( 0 ) ; static Semaphore mutex = new Semaphore ( 1 ) ; // I'll producing new Integer every time static class CrunchifyProducer extends Thread { public void run ( ) { int counter = 1 ; try { while ( true ) { String threadName = Thread . currentThread ( ) . getName ( ) + counter ++ ; mutex . acquire ( ) ; crunchifyList . add ( threadName ) ; System . out . println ( "Producer is prdoucing new value: " + threadName ) ; mutex . release ( ) ; // release lock semaphore . release ( ) ; Thread . sleep ( 500 ) ; } } catch ( Exception x ) { x . printStackTrace ( ) ; } } } // I'll be consuming Integer every stime static class CrunchifyConsumer extends Thread { String consumerName ; public CrunchifyConsumer ( String name ) { this . consumerName = name ; } public void run ( ) { try { while ( true ) { // acquire lock. Acquires the given number of permits from this semaphore, blocking until all are // available // process stops here until producer releases the lock semaphore . acquire ( ) ; // Acquires a permit from this semaphore, blocking until one is available mutex . acquire ( ) ; String result = "" ; for ( String value : crunchifyList ) { result = value + "," ; } System . out . println ( consumerName + " consumes value: " + result + "crunchifyList.size(): " + crunchifyList . size ( ) + "\n" ) ; mutex . release ( ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } } } public static void main ( String [ ] args ) { new CrunchifyProducer ( ) . start ( ) ; new CrunchifyConsumer ( "Crunchify" ) . start ( ) ; new CrunchifyConsumer ( "Google" ) . start ( ) ; new CrunchifyConsumer ( "Yahoo" ) . start ( ) ; } } |
在上面的教程CrunchifySemaphoreMutexTutorial.java
,当CrunchifyProducer
将threadName
添加到crunchifyList
linkedList 对象时,它可以向信号量发出信号。
然后, CrunchifyConsumer
可以尝试获取信号量,因此它们将一直等待,直到 CrunchifyProducer 发出已添加线程 ID 的信号。 在发出添加数据的信号后,其中一个消费者将被唤醒,它会知道它可以读取一个 crunchifyList 对象。 它可以读取一个列表,然后返回尝试获取信号量。
如果在那个时候,生产者已经写了另一个数据包,它会再次发出信号,然后任何一个消费者将继续读取另一个数据包,依此类推……
换一种说法:
1 2 3 |
CrunchifyProducer : Add an object o List - Semaphore . release ( 1 ) CrunchifyConsumer x N ) - Semaphore . acquire ( 1 ) - Read an object from List |

结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Producer is prdoucing new value : Thread - 01 Crunchify consumes value : Thread - 01 , crunchifyList . size ( ) : 1 Producer is prdoucing new value : Thread - 02 Google consumes value : Thread - 02 , crunchifyList . size ( ) : 2 Producer is prdoucing new value : Thread - 03 Yahoo consumes value : Thread - 03 , crunchifyList . size ( ) : 3 Producer is prdoucing new value : Thread - 04 Crunchify consumes value : Thread - 04 , crunchifyList . size ( ) : 4 . . . . . . . . . . . . . . . |
如何防止竞态条件:
What if you have multiple Consumers?
在上面的 Java 教程中,消费者(不是生产者)在读取数据包时(而不是在获取信号量时)应该锁定缓冲区,以防止竞争条件。 在下面的示例中,生产者还锁定了列表,因为所有内容都在同一个 JVM 上。
示例 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package crunchify . com . tutorial ; import java . util . concurrent . Semaphore ; /** * @author Crunchify.com * */ public class CrunchifyJavaSemaphoreTutorial { private static final int MAX_CONCURRENT_THREADS = 2 ; private final Semaphore crunchifyAdminLOCK = new Semaphore ( MAX_CONCURRENT_THREADS , true ) ; public void crunchifyStartTest ( ) { for ( int i = 0 ; i < 10 ; i ++ ) { CrunchifyPerson person = new CrunchifyPerson ( ) ; person . start ( ) ; } } public class CrunchifyPerson extends Thread { @Override public void run ( ) { try { // Acquire Lock crunchifyAdminLOCK . acquire ( ) ; } catch ( InterruptedException e ) { System . out . println ( "received InterruptedException" ) ; return ; } System . out . println ( "Thread " + this . getId ( ) + " start using Crunchify's car - Acquire()" ) ; try { sleep ( 1000 ) ; } catch ( Exception e ) { } finally { // Release Lock crunchifyAdminLOCK . release ( ) ; } System . out . println ( "Thread " + this . getId ( ) + " stops using Crunchify's car - Release()\n" ) ; } } public static void main ( String [ ] args ) { CrunchifyJavaSemaphoreTutorial test = new CrunchifyJavaSemaphoreTutorial ( ) ; test . crunchifyStartTest ( ) ; } } |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Thread 11 start using Crunchify 's car - Acquire() Thread 10 start using Crunchify' s car - Acquire ( ) Thread 10 stops using Crunchify 's car - Release() Thread 12 start using Crunchify' s car - Acquire ( ) Thread 13 start using Crunchify 's car - Acquire() Thread 11 stops using Crunchify' s car - Release ( ) Thread 13 stops using Crunchify 's car - Release() Thread 15 start using Crunchify' s car - Acquire ( ) Thread 14 start using Crunchify 's car - Acquire() Thread 12 stops using Crunchify' s car - Release ( ) Thread 14 stops using Crunchify 's car - Release() Thread 16 start using Crunchify' s car - Acquire ( ) Thread 15 stops using Crunchify 's car - Release() Thread 17 start using Crunchify' s car - Acquire ( ) Thread 17 stops using Crunchify 's car - Release() Thread 18 start using Crunchify' s car - Acquire ( ) Thread 19 start using Crunchify 's car - Acquire() Thread 16 stops using Crunchify' s car - Release ( ) Thread 18 stops using Crunchify 's car - Release() Thread 19 stops using Crunchify' s car - Release ( ) |