面试题答案
一键面试优化思路
- 公平性设置:
Semaphore
默认是非公平的,可能导致某些线程长时间等待。将其设置为公平模式,按照线程请求的顺序来分配信号量,减少饥饿现象。 - 动态调整:根据实际的连接使用情况,动态调整信号量的数量。如果发现连接长时间被占用,且等待队列较长,可以适当增加信号量数量;反之,如果连接利用率较低,可以适当减少信号量数量。
- 超时机制:为获取信号量的操作设置超时时间,避免线程无限期等待。
优化代码片段
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class DatabaseConnectionPool {
private static final int MAX_CONNECTIONS = 10;
private final Semaphore semaphore;
// 模拟数据库连接
private boolean[] connections;
public DatabaseConnectionPool() {
// 设置为公平模式
semaphore = new Semaphore(MAX_CONNECTIONS, true);
connections = new boolean[MAX_CONNECTIONS];
}
public boolean getConnection(long timeout) {
try {
// 设置获取信号量的超时时间
if (semaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (!connections[i]) {
connections[i] = true;
return true;
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}
public void releaseConnection() {
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (connections[i]) {
connections[i] = false;
semaphore.release();
break;
}
}
}
}
在上述代码中:
Semaphore
构造函数中设置为公平模式new Semaphore(MAX_CONNECTIONS, true)
。getConnection
方法中使用tryAcquire(long timeout, TimeUnit unit)
设置了获取信号量的超时时间,避免线程无限期等待。releaseConnection
方法在释放连接时,释放对应的信号量,使得其他等待的线程有机会获取连接。