HttpUrlConnection 造成死锁

本人纯属虚构 发布于 2013/08/30 13:31
阅读 1K+
收藏 2
请问下各位 我用java自带的 HttpUrlConnection
用jstack导出信息发现有死锁
conn.getHeaderFields().get("Set-Cookie")
getHeader 也会有锁 这是神马情况?


导出的信息里面有很多这样的死锁线程


"Thread-421" prio=6 tid=0x08ae4400 nid=0x1540 runnable [0x065cf000]
   java.lang.Thread.State: RUNNABLE
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.read(SocketInputStream.java:129)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
	at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
	- locked <0x2871d648> (a java.io.BufferedInputStream)
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:698)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:641)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1218)
	- locked <0x2871d680> (a sun.net.www.protocol.http.HttpURLConnection)
	at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(HttpURLConnection.java:2362)
	at com.horn.util.MsgManage.sendHttpMessage(MsgManage.java:1003)
	at com.horn.util.MsgManage.sendHttpMessage(MsgManage.java:954)
	at com.horn.util.PollMessageThread.run(PollMessageThread.java:63)

   Locked ownable synchronizers:
	- None



加载中
0
winnie_tour
winnie_tour
你贴下你测试的代码,我帮你看下
本人纯属虚构
本人纯属虚构
方法里的 Map<String, String> headers 可以为null
本人纯属虚构
本人纯属虚构
感谢 我这个是写的一个方法 我把这个方法贴出来吧 不知道好不好看
0
本人纯属虚构
本人纯属虚构
public String sendHttpMessage(String url, String method, String contents,
			Map<String, String> headers) {
		try {
			URL serverUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) serverUrl
					.openConnection();
			conn.setConnectTimeout(20000);
			HttpURLConnection.setFollowRedirects(false);
			conn.setRequestMethod(method);// "POST" ,"GET"
			conn
					.addRequestProperty("Accept",
							"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

			String s = "";
			for (Cookie c : cookies.values()) {
				if (c.getName() != null) {
					s += c.getName() + "=" + c.getValue() + "; ";
				}
			}
			if (!s.isEmpty()) {
				conn.addRequestProperty("Cookie", s);
			}
			if (null != refer)
				conn.addRequestProperty("Referer", refer);
			conn.addRequestProperty("Accept-Language",
					"zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4");
			conn.addRequestProperty("Accept-Encoding", "gzip, deflate");
			conn.addRequestProperty("Cache-Control", "no-cache");
			conn
					.addRequestProperty("Accept-Charset",
							"GBK,utf-8;q=0.7,*;q=0.3");
			conn
					.addRequestProperty(
							"User-Agent",
							"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36 CoolNovo/2.0.9.16");
			if (method.equalsIgnoreCase(Method.GET.name())) {
				conn.connect();
			} else if (method.equalsIgnoreCase(Method.POST.name())) {

				conn.setDoOutput(true);
				conn.connect();
				conn.getOutputStream().write(contents.getBytes());
			} else {
				throw new RuntimeException("your method is not implement");
			}

			if (conn.getHeaderFields().get("Set-Cookie") != null) {
				for (String ss : conn.getHeaderFields().get("Set-Cookie")) {
					Cookie c = new Cookie(ss);
					cookies.put(c.getName(), c);
				}

			}

			if (headers != null) {
				headers.put("Location", conn.getHeaderField("Location"));
			}

			InputStream ins = conn.getInputStream();

			// 处理GZIP压缩的
			if (null != conn.getHeaderField("Content-Encoding")
					&& conn.getHeaderField("Content-Encoding").equals("gzip")) {
				byte[] b = null;
				GZIPInputStream gzip = new GZIPInputStream(ins);
				byte[] buf = new byte[1024];
				int num = -1;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while ((num = gzip.read(buf, 0, buf.length)) != -1) {
					baos.write(buf, 0, num);
				}
				b = baos.toByteArray();
				baos.flush();
				baos.close();
				gzip.close();
				ins.close();
				return new String(b, "UTF-8").trim();
			}

			String charset = "UTF-8";
			InputStreamReader inr = new InputStreamReader(ins, charset);
			BufferedReader br = new BufferedReader(inr);

			String line = "";
			StringBuffer sb = new StringBuffer();
			do {
				sb.append(line);
				line = br.readLine();
			} while (line != null);
			// Log.println("response=" + sb);
			return sb.toString();
		} catch (Exception e) {
			e.getMessage();
			return "";
		}
	}
0
winnie_tour
winnie_tour
调用 sendHttpMessage这个方法的时候是怎么调用的?
本人纯属虚构
本人纯属虚构
忘了说明,平时用没问题,就导出信息的时候看到有 locked
本人纯属虚构
本人纯属虚构
直接调用的 比如 String result = sendHttpMessage("http://www.baidu.com", "GET", null);
返回顶部
顶部