入职了一家做金融大数据的公司,终于走向Java开发者的正轨了~
最近遇到一个问题,服务器多线程程序在运行时,偶尔线程会发生死锁,进程在内存中不能释放,该程序会先删除数据库的表,执行完毕会新生成数据。这样就导致了数据库的表被清空,于是开始调查:
ps -aux|grep appps -eLf|grep appjstack -l pid > pid.log
找到了问题所在,就是在 调用java 的 获取线上数据时,发生超时了:
at java.io.BufferedInputStream.read(BufferedInputStream.java:345) - locked <0x0000000674b25db8> (a java.io.BufferedInputStream) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1535) - locked <0x0000000674b1a7a0> (a sun.net.www.protocol.http.HttpURLConnection) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440) - locked <0x0000000674b1a7a0> (a sun.net.www.protocol.http.HttpURLConnection) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
解决方案:
第一步追加 设置从主机读取的超时设置
URL url = new URL(urlStr);HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();httpConn.setConnectTimeout(3000);httpConn.setReadTimeout(3000);//addhttpConn.setDoInput(true);httpConn.setRequestMethod("GET");httpConn.connect();//add
第二步追加多次链接尝试:
json = getJsonContent(URL);int i = 0;while (StringUtils.isEmpty(json) && i < 3) { json = getJsonContent(URL); i++;}