从 Web 服务下载文件时线程卡在 IN_NATIVE 状态
Thread stuck in IN_NATIVE state while downloading a file from webservice
我正在通过我的代码从 X 服务下载一个文件。下载的文件写入文件系统。
我面临的问题是我的实例卡住了,没有进一步处理作业。在线程转储中,我看到大多数线程处于 IN_NATIVE 状态。
你能帮我看看哪里错了吗?
Thread 25493: (state = IN_NATIVE)
- java.net.SocketInputStream.socketRead0(java.io.FileDescriptor, byte[], int, int, int) @bci=0 (Compiled frame; information may be imprecise)
- java.net.SocketInputStream.read(byte[], int, int, int) @bci=87, line=152 (Compiled frame)
- java.net.SocketInputStream.read(byte[], int, int) @bci=11, line=122 (Compiled frame)
- sun.security.ssl.InputRecord.readFully(java.io.InputStream, byte[], int, int) @bci=21, line=442 (Compiled frame)
- sun.security.ssl.InputRecord.read(java.io.InputStream, java.io.OutputStream) @bci=32, line=480 (Interpreted frame)
- sun.security.ssl.SSLSocketImpl.readRecord(sun.security.ssl.InputRecord, boolean) @bci=44, line=927 (Interpreted frame)
- sun.security.ssl.SSLSocketImpl.readDataRecord(sun.security.ssl.InputRecord) @bci=15, line=884 (Interpreted frame)
- sun.security.ssl.AppInputStream.read(byte[], int, int) @bci=72, line=102 (Interpreted frame)
- org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer() @bci=71, line=160 (Compiled frame)
- org.apache.http.impl.io.SocketInputBuffer.fillBuffer() @bci=1, line=84 (Compiled frame)
- org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(org.apache.http.util.CharArrayBuffer) @bci=130, line=273 (Compiled frame)
- org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(org.apache.http.io.SessionInputBuffer) @bci=16, line=140 (Compiled frame)
- org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(org.apache.http.io.SessionInputBuffer) @bci=2, line=57 (Compiled frame)
- org.apache.http.impl.io.AbstractMessageParser.parse() @bci=38, line=260 (Compiled frame)
- org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader() @bci=8, line=283 (Compiled frame)
- org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader() @bci=1, line=251 (Compiled frame)
- org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader() @bci=6, line=197 (Compiled frame)
- org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(org.apache.http.HttpRequest, org.apache.http.HttpClientConnection, org.apache.http.protocol.HttpContext) @bci=41, line=271 (Compiled frame)
下面是处理来自 X 服务的输入流的代码
public void handleResponse(Response response, InputStream responseStream) throws IOException {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
if (response != null && responseStream != null) {
bis = new java.io.BufferedInputStream(responseStream);
//code writing to disk
}
}
catch (IOException e) {
log.error("IOException while writing file " + fileName + " to disk ", e);
}
catch (Exception e) {
log.error("IOException while writing file " + fileName + " to disk ", e);
}
finally {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
}
}
流正在等待来自服务的更多数据。
也许服务没有正确关闭连接,或者你没有检测到流已经结束(读取字节直到它发送 -1)。
我正在通过我的代码从 X 服务下载一个文件。下载的文件写入文件系统。 我面临的问题是我的实例卡住了,没有进一步处理作业。在线程转储中,我看到大多数线程处于 IN_NATIVE 状态。
你能帮我看看哪里错了吗?
Thread 25493: (state = IN_NATIVE)
- java.net.SocketInputStream.socketRead0(java.io.FileDescriptor, byte[], int, int, int) @bci=0 (Compiled frame; information may be imprecise)
- java.net.SocketInputStream.read(byte[], int, int, int) @bci=87, line=152 (Compiled frame)
- java.net.SocketInputStream.read(byte[], int, int) @bci=11, line=122 (Compiled frame)
- sun.security.ssl.InputRecord.readFully(java.io.InputStream, byte[], int, int) @bci=21, line=442 (Compiled frame)
- sun.security.ssl.InputRecord.read(java.io.InputStream, java.io.OutputStream) @bci=32, line=480 (Interpreted frame)
- sun.security.ssl.SSLSocketImpl.readRecord(sun.security.ssl.InputRecord, boolean) @bci=44, line=927 (Interpreted frame)
- sun.security.ssl.SSLSocketImpl.readDataRecord(sun.security.ssl.InputRecord) @bci=15, line=884 (Interpreted frame)
- sun.security.ssl.AppInputStream.read(byte[], int, int) @bci=72, line=102 (Interpreted frame)
- org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer() @bci=71, line=160 (Compiled frame)
- org.apache.http.impl.io.SocketInputBuffer.fillBuffer() @bci=1, line=84 (Compiled frame)
- org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(org.apache.http.util.CharArrayBuffer) @bci=130, line=273 (Compiled frame)
- org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(org.apache.http.io.SessionInputBuffer) @bci=16, line=140 (Compiled frame)
- org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(org.apache.http.io.SessionInputBuffer) @bci=2, line=57 (Compiled frame)
- org.apache.http.impl.io.AbstractMessageParser.parse() @bci=38, line=260 (Compiled frame)
- org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader() @bci=8, line=283 (Compiled frame)
- org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader() @bci=1, line=251 (Compiled frame)
- org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader() @bci=6, line=197 (Compiled frame)
- org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(org.apache.http.HttpRequest, org.apache.http.HttpClientConnection, org.apache.http.protocol.HttpContext) @bci=41, line=271 (Compiled frame)
下面是处理来自 X 服务的输入流的代码
public void handleResponse(Response response, InputStream responseStream) throws IOException {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
if (response != null && responseStream != null) {
bis = new java.io.BufferedInputStream(responseStream);
//code writing to disk
}
}
catch (IOException e) {
log.error("IOException while writing file " + fileName + " to disk ", e);
}
catch (Exception e) {
log.error("IOException while writing file " + fileName + " to disk ", e);
}
finally {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
}
}
流正在等待来自服务的更多数据。 也许服务没有正确关闭连接,或者你没有检测到流已经结束(读取字节直到它发送 -1)。