贴一段代码:
public void releaseConn(Connection conn) throws SQLException {
//log.info(Thread.currentThread().getName() + "关闭连接:activeConnections.remove:" + conn);
this.activeConnections.remove(conn);
this.currentConnection.remove();
try {
if (isValidConnection(conn)) {
this.freeConnections.add(conn);
}else{
freeConnections.add(this.NewConnection());
}
} catch (SQLException e) {
e.printStackTrace();
}
// 唤醒getConnection()中等待的线程
this.notifyAll();
}
该程序运行会报 java.lang.IllegalMonitorStateException
解决:
在调用
this.notifyAll();
时,需要给该方法或代码段添加同步控制语句synchronized。
以上方法,在声明时加上synchronized即解决问题