Thread starting during runtime shutdown问题的解决

注:本内容仅是工作笔记,用于备忘,未贴出具体代码。描述不清请见谅。

===================================================================================================

问题描述与分析:

  为了获取Crash日志,项目中实现了UncaughtExceptionHandler接口对未知异常进行捕获并上传到服务器中,同时停止App运行。在Android5.0以下系统一直未出现过问题,但是突然发现在Android 5.0以上的机器中这段代码出现了java.lang.InternalError: Thread starting during runtime shutdown(更多异常信息见最后部分的附录),在stackoverflow中得到了一些帮助:这个问题是因为线程开启的太晚了!

  什么时候开启线程才算得上“太晚了”呢? 在我的项目中UncaughtExceptionHandler实现类捕获到异常时(也就是UncaughtExceptionHandler接口的uncaughtException方法执行时),开启了一个线程用于上传崩溃日志,而在线程中上传崩溃日志时创建了HttpClient发送网络请求。HttpClient创建时设置了ThreadSafeClientConnManager来管理连接,而在ThreadSafeClientConnManager的源码中又一次开启了线程!也就是说,uncaughtException方法执行时在开启的线程中又开启了新的线程,这样可能会导致uncaughtException方法在执行完成时,“线程中的线程”才start(),而据说uncaughtException方法执行完成后ART环境就shutdown了(uncaughtException方法结束时ART是否会真的shutdown,这一点有待考证,笔者也仅是从其他非官方资料中得知),这种情况下就会抛出java.lang.InternalError: Thread starting during runtime shutdown。

  为了验证这个问题,编写了一段简单的代码:实现UncaughtExceptionHandler的uncaughtException方法,在uncaughtException方法中开启一个线程,在这个线程中再开一线程,同时在MainActivity中故意制造一个异常发生,此时果然会出现java.lang.InternalError: Thread starting during runtime shutdown(并不是每次都出现,因为如果uncaughtException方法执行结束之前,两个线程都完成start(),此问题就不会出现)。

解决方法:

  避免在uncaughtException方法中出现线程嵌套:针对我的项目,不再去在线程中创建HttpClient(因为HttpClient创建时还会开启线程,这就造成了线程中再次开启线程),而是先创建HttpClient对象,再在线程中使用HttpClient对象。这样就可以确保uncaughtException方法结束之前HttpClient已经被创建,HttpClient中开启线程也就不会有问题。

附异常信息:

java.lang.InternalError: Thread starting during runtime shutdown at java.lang.Thread.nativeCreate(Native Method) at java.lang.Thread.start(Thread.java:1042) at org.apache.http.impl.conn.tsccm.AbstractConnPool.enableConnectionGC(AbstractConnPool.java:140) at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.createConnectionPool(ThreadSafeClientConnManager.java:120) at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>(ThreadSafeClientConnManager.java:98) at

时间: 2024-08-11 01:44:10

Thread starting during runtime shutdown问题的解决的相关文章

main thread starting…

执行结果例如以下: main thread starting- Thrad 2 staring- Thrad 2 end- Thrad 4 staring- Thrad 4 end- Thrad 1 staring- Thrad 1 end- Thrad 3 staring- Thrad 3 end- Thrad 5 staring- Thrad 5 end- main thread end- CountDownLatch方式代码例如以下: package com.test.thread; im

Type cvc-complex-type.2.4.a: Invalid content was found starting with element &#39;build&#39;.错误的解决方法

项目突然间爆出了这样的问题: Description Resource Path Location Typecvc-complex-type.2.4.a: Invalid content was found starting with element 'build'. One of '{"http://maven.apache.org/POM/4.0.0":parent, "http://maven.apache.org/POM/4.0.0":name, "

No EntityManager with actual transaction available for current thread - cannot reliably process &#39;remove&#39; call的解决办法

No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call 遇到这种问题,应该是你的controller层直接调用了Repository的删除方法,类似的可能是controller层直接调用了Repository的增加或修改方法.如果项目封装了controller不能直接调用Repository的删除.增加和修改方法,那么在control

Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier解决办法

使用Storyboard时出现以下警告: warning: Unsupported Configuration: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:. 大意是为了在程序中动态访问Scene,需要给其设置一个Storyboard ID,所以给出了

Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated ?

Thread.stop, Thread.suspend, Thread.resume被标记为废弃的方法.在查看JDK的文档时,提到了下面的参考文章,先是英文版,接着是中文翻译. Why is Thread.stop deprecated?Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unloc

Zend Guard Run-time support missing!的解决办法

问题:安装新版本wordlpress的时候遇到如下问题, Zend Guard Run-time support missing! One more more files on this web site were encoded by ZendGuard and the required run-time support is not installed orproperly configured.For the Web site user This means that this Web s

ora-01033:oracle initialization or shutdown in progress 解决方法

今天研究Oracle遇到了这个问题ora-01033:oracle initialization or shutdown in progress,经过分析研究终于解决了,写下来纪念一下.我的库是oracle 9i,具体就是90的.一.首先:问题的产生原因,出现这个错误是因为我将oracle\oradata\oradb下的一个文件误删除掉后出现的.二.现象:SQL*Plus无法连接,显示以下错误:ORA-01033: ORACLE initialization or shutdown in pro

oracle 11g错误ora-01033:oracle initialization or shutdown in progress解决办法

原文出自:http://blog.csdn.net/liverliu/article/details/6410287 一.首先:问题的产生原因,先前我在f:/llh/目录创建的一个bookspace表空间,后来不想要这个表空间了,所以我就直接删掉了,出现这个错误是因为我将f:/llh/bookspace.dbf等几个文件误删除掉后出现的. 二.现象:SQL*Plus无法连接,显示以下错误: ORA-01033: Oracle initialization or shutdown in progr

oracle initialization or shutdown in progress解决方法

[解决方法] SQL> connect sys/hope as sysdba 已连接. SQL> shutdown normal ORA-01109: 数据库未打开 已经卸载数据库. ORACLE 例程已经关闭. SQL> start mount SP2-0310: 无法打开文件 "mount.sql" SQL> startup mount ORACLE 例程已经启动. Total System Global Area  612368384 bytes Fixe