一個線程泄漏問題的定位

一條異常日志

[Error][.CinSocketHandlerThread][run][58][SYS_101][1000][433957346][5.1.1|LMY47V

release-keys|2017042817|5.2.25|SMARTISAN|YQ601][4|NETWORK_TYPE_LTE][]Socket添加任務隊列異常,errorMsg:java.lang.InterruptedException

at? java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:1991)

at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2025)

atjava.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:410)

atcom.allstar.cinclient.socket.CinSocketHandlerThread.run(:49)


代碼

LinkedBlockingQueue

@Override

public

voidrun()

{

CinClient.attachCurrentThreadUncatchExceptionHandler();

while(CinClient.isMainProcessRunning())

{

CinSocketTasktask

=null;

try

{

task =_queue.take();

if(task

!=null&&task.Trans!=null)

{

Trace.T(this.hashCode());

task.Trans.receiveResponse(task.Response);

}

}

catch(InterruptedExceptione)

{

LogOpr.error("Socket添加任務隊列異常,errorMsg:"+Log.getStackTraceString(e),ErrorCode.SYS_TCP,"",LogOpr.getFileName(),LogOpr.getMethodName(),LogOpr.getLineNumber());

}

catch(Exceptione)

{

LogOpr.error("Socket添加任務隊列異常,errorMsg:"+Log.getStackTraceString(e),ErrorCode.SYS_TCP,"",LogOpr.getFileName(),LogOpr.getMethodName(),LogOpr.getLineNumber());

}

}

System.out.println("----------Task

STOP!!------");

}



產生異常的源頭

CinSocketHandlerThreadPool.java

//logon時調用

public static voidinitialize(intlength){

if(_threads!=null){

stop();

}

_threads=newCinSocketHandlerThread[length];

for(inti=0;i<

length;i++){

_threads[i] =newCinSocketHandlerThread();

_threads[i].start();

}

}

public static voidstop(){

for(Thread t :_threads){

t.interrupt();

}

}



app異常現象:每次登陸,都會泄露5個CinSocketHandlerThread線程,該線程是用來處理網絡響應的



延伸:什么時候,線程會中斷

1.線程在運行中,不會中斷,如果其他線程調用了該線程的interrupt方法,只是對該線程的interrupt標志位置位,該線程能通過

Thread.currentThread().isInterrupted()來訪問該標志。

2.如果該線程在執行一個低級可中斷阻塞方法,例如 Thread.sleep()、

Thread.join() 或

Object.wait(),那么它將取消阻塞并拋出

InterruptedException。

3.并非所有的阻塞方法都拋出?InterruptedException。輸入和輸出流類會阻塞等待 I/O 完成,但是它們不拋出InterruptedException,而且在被中斷的情況下也不會提前返回。

https://www.ibm.com/developerworks/cn/java/j-jtp05236.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容