Multi-Programming-12 Semaphore信号量机制

时间:2021-01-20 15:18:43

1.Semaphore 和 ReentrantLock 的condition有什么不同(acquire()/release()、await()/signal())?

这里有关于此问题的详尽问答,下面贴下截图。 Multi-Programming-12 Semaphore信号量机制
Multi-Programming-12 Semaphore信号量机制

大意:java.util.concurrent.Semaphore是相对高级的同步机制,一般用来限制并发访问同一资源的线程数量,通过每个线程访问资源之前调用acquire()方法,这样会使permits数量-1.访问结束之后调用release()方法,这样会使permits数量+1. java.util.concurrent.locks.Condition是相对来说低级别的同步机制,可以看作增强版本的java.lang.Object notify() and wait()。

2.Semaphore 和 ReentrantLock不同?

这里有关于该问题的问答。 Multi-Programming-12 Semaphore信号量机制

3.信号量Demo和结果

package com.fqy.mix;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo {
public static void main(String[] args) {
SemaphoreDemoUtil.demonstrate();
}

}

class SemaphoreDemoUtil {
public static void demonstrate() {
Long startTime = System.currentTimeMillis();
Connection connection = Connection.getInstance();
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 30; i++) {
executor.submit(new Runnable() {

@Override
public void run() {
try {
connection.doConnect();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Long endTime = System.currentTimeMillis();
System.out.println("Total taken time is: " + (endTime - startTime));
}
}

class Connection {
private int count = 0;
private Semaphore semaphore = new Semaphore(10);

private Connection() {

}

private static final Connection INSTANCE = new Connection();

public static Connection getInstance() {
return INSTANCE;
}

private void connect() {
synchronized (this) {
count++;
System.out.println("Current connections is: " + count);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
count--;
}
}

public void doConnect() throws InterruptedException {
try {
semaphore.acquire();
connect();
} finally {
semaphore.release();
}

}
}

运行结果:
Current connections is: 1
Current connections is: 2
Current connections is: 3
Current connections is: 4
Current connections is: 5
Current connections is: 6
Current connections is: 7
Current connections is: 8
Current connections is: 9
Current connections is: 10
Current connections is: 1
Current connections is: 2
Current connections is: 3
Current connections is: 4
Current connections is: 5
Current connections is: 6
Current connections is: 7
Current connections is: 8
Current connections is: 9
Current connections is: 10
Current connections is: 1
Current connections is: 2
Current connections is: 3
Current connections is: 4
Current connections is: 5
Current connections is: 6
Current connections is: 7
Current connections is: 8
Current connections is: 9
Current connections is: 10
Total taken time is: 3013


4.己见

Semaphore机制是限制访问同一资源线程数量的,是一种更高级别的机制,可以和ReentrantLock等配合使用。 这里可以获取相关笔记和代码。