Thread starting during runtime shutdown problem solving

Source: Internet
Author: User

Note: This content is only a work note, used for memo, does not post specific code. The description is not clear please forgive me.

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

Problem description and Analysis:

In order to obtain the crash log, the project implements the Uncaughtexceptionhandler interface to capture the unknown exception and upload it to the server, while stopping the app from running. Android5.0 the following system has not been the problem, but suddenly found in Android 5.0 above the machine in the code appeared java.lang.InternalError:Thread starting during runtime Shutdown (see the Appendix to the last section for more information about exceptions) and get some help in StackOverflow: The problem is that threads are too late to open!

When is it "too late" to start a thread? When a Uncaughtexceptionhandler implementation class catches an exception in my project (that is, when the Uncaughtexception method of the Uncaughtexceptionhandler interface executes), a thread is turned on to upload the crash log, The HttpClient send network request is created when the crash log is uploaded in the thread. HttpClient created when the Threadsafeclientconnmanager to manage the connection, and in the Threadsafeclientconnmanager source code again opened the thread! That is, the Uncaughtexception method executes when a new thread is opened in the open thread, which may cause the Uncaughtexception method to start () thread in thread when execution completes. It is said that the Uncaughtexception method after the completion of the art environment is shutdown (Uncaughtexception method at the end of the art will be really shutdown, this is to be verified, the author is only from other unofficial information learned), In this case, the Java.lang.InternalError:Thread starting during runtime shutdown will be thrown.

To verify the problem, write a simple code: Implement the Uncaughtexceptionhandler Uncaughtexception method, open a thread in the Uncaughtexception method, and then open a thread At the same time in the Mainactivity deliberately create an exception occurred, this time will appear java.lang.InternalError:Thread starting during runtime shutdown (not every time, This problem does not occur because two threads complete start () before the Uncaughtexception method execution is completed.

Workaround:

Avoid thread nesting in the Uncaughtexception method: For my project, I'm not going to create httpclient in the thread (because HttpClient also turns on threads when created, which causes threads to be turned on again), Instead, the HttpClient object is created first, and then the HttpClient object is used in the thread. This ensures that HttpClient has been created before the Uncaughtexception method is finished, and that there is no problem opening the thread in HttpClient.

Attached exception information:

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

Thread starting during runtime shutdown problem solving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.