我有一个免费套接字conn的列表,然后我把它放在一个Map中,如果状态为free或inUse,但同步的dosent seens正常工作 这是我的代码: 我的地图是这样的:
private ConcurrentHashMap<MVConnection, Boolean> listaConn = new ConcurrentHashMap<>();
我的connManager是这样的:
public synchronized MVConnection getInstance() {
System.out.println("pass here on getInstance");
System.out.println("--------------------------------------------------------");
System.out.println("Before request Instance");
LogLinhas();
System.out.println("--------------------------------------------------------");
MVConnection searchResult = null;
System.out.println(Thread.currentThread().getName() + " is running");
while (searchResult == null) {
searchResult = this.listaConn.search(1, (conn, free) -> {
if (free) {
return conn;
}
return null;
});
}
notify();
System.out.println(Thread.currentThread().getName() + " notifying");
if (searchResult != null) {
this.listaConn.replace(searchResult, false);
try {
System.out.println("Set " + searchResult.getServerPort() + " as busy");
} catch (MVException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("--------------------------------------------------------");
System.out.println("After request Instance");
LogLinhas();
System.out.println("--------------------------------------------------------");
return searchResult;
}
和cont的请求函数是这样的:
public abstract class AbstractD3Dao<T extends Serializable> {
@Autowired
ConexaoD3 conexao;
protected MVConnection getCurrentSession() {
System.out.println("Abstract request the conn");
MVConnection connPool = null;
synchronized (conexao.getInstance()) {
while (connPool == null) {
try {
System.out.println("Aguardando Conexao");
System.out.println(Thread.currentThread().getName() + " is waiting");
wait();
} catch (InterruptedException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
connPool = conexao.getInstance();
}
}
try {
System.out.println(" Abstract assineg the PIB " + connPool.getServerPort() + "to use");
} catch (MVException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connPool;
}
`