老师有没有讲解死锁的教程啊,请教学
final NioSocketConnector connector = getConnector();
connector.getFilterChain().addLast("transportCoder",
new ProtocolCodecFilter(new CustomProtocolCodecFactory(Charset.forName("UTF-8"))));
connector.getSessionConfig().setReceiveBufferSize(2048);//接收缓冲区1M
connector.setConnectTimeoutMillis(ConfigBean.getHeartbeatTimeout()); // 设置连接超时
connector.setHandler(new TcpClientHandler());// 设置消息处理器
connector.getFilterChain().addLast("exceutor", new ExecutorFilter());
//=============添加监控,断线重连============
connector.setDefaultRemoteAddress(new InetSocketAddress(ConfigBean.getOpenRestyUrl(),ConfigBean.getOpenRestyPort()));
connector.addListener(new IoListener(){
@Override
public void sessionDestroyed(IoSession arg)throws Exception{
cfs=null;
for (;;) {
if (isHostConnectable(ConfigBean.getOpenRestyUrl(), ConfigBean.getOpenRestyPort())) {
try {
Thread.sleep(5000);
ConnectFuture future = connector.connect();
future.awaitUninterruptibly();// 等待连接创建完成 这里可能出现死锁
session = future.getSession();
if (session.isConnected()) {
cfs=future;
logger.info("断线重连[" + connector.getDefaultRemoteAddress().getHostName() + ":" + connector.getDefaultRemoteAddress().getPort() + "]成功");
break;
}
} catch (Exception e) {
cfs=null;
logger.error("重连服务器登录失败,5秒再连接一次:" + e.getMessage());
}
}
}
}
});
之前看书有讲:1、尽量避免不必要的synchronized关键字。2、可以用其他方法替换synchronized关键字,比如标志不可变量。3、保证synchronized代码块简练。个人表述不太清楚,详见《Java Network Programming》第四版Deadlock块部分,谢谢。