这个Bug发生在这样的环境:你有很多的线程在同时运行,并且每个线程都需要持续运行相当长一些时间; 其中一些是用AsyncTask去作的,并且AsyncTask在其他的线程执行顺序之后。

java.lang.NoClassDefFoundError:android.os.AsyncTask.这里暂时不贴出异常栈信息(出现在uncaughtException),通过大量的search,终于找到一些有营养的资料:

Received: by 10.101.85.19 with SMTP id n19mr4167516anl.30.1314658242480;        Mon, 29 Aug 2011 15:50:42 -0700 (PDT)X-BeenThere: android-developers@googlegroups.comReceived: by 10.101.181.21 with SMTP id i21ls13340762anp.4.gmail; Mon, 29 Aug 2011 15:48:59 -0700 (PDT)Received: by 10.101.24.5 with SMTP id b5mr2043100anj.37.1314658139241;        Mon, 29 Aug 2011 15:48:59 -0700 (PDT)Received: by 10.151.138.5 with SMTP id q5msybn;        Mon, 29 Aug 2011 10:44:15 -0700 (PDT)Received: by 10.101.168.1 with SMTP id v1mr728239ano.36.1314639855438;        Mon, 29 Aug 2011 10:44:15 -0700 (PDT)Date: Mon, 29 Aug 2011 10:44:14 -0700 (PDT)From: exakoustos <stratouk...@gmail.com>Reply-To: android-developers@googlegroups.comTo: android-developers@googlegroups.comMessage-ID: <28014072.5885.1314639854428.JavaMail.geo-discussion-forums@yqgc10>In-Reply-To: <3208001.1407.1314283401379.JavaMail.geo-discussion-forums@yqcd38>References: <0146c2a8-4093-4416-b2c4-d0698109cb95@a12g2000yqi.googlegroups.com> <CANCScgjEZ1YEHL+R9srFEkoA7wft_s=LzDpcmLnyHWTa_NM2_A@mail.gmail.com> <3abc056a-a09d-4a64-a341-a1064cd988ee@z17g2000vbp.googlegroups.com> <CALLdnhPdcfScNw7cScT_fcKKSR-S08qjGtbyVTb-JrQjwkbd2g@mail.gmail.com> <3208001.1407.1314283401379.JavaMail.geo-discussion-forums@yqcd38>Subject: Re: [android-developers] Re: java.lang.NoClassDefFoundError: android.os.AsyncTaskMIME-Version: 1.0Content-Type: multipart/mixed; boundary="----=_Part_5883_18950387.1314639854427"------=_Part_5883_18950387.1314639854427Content-Type: multipart/alternative; boundary="----=_Part_5884_488802.1314639854427"------=_Part_5884_488802.1314639854427Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 7bitWas faced with the same error when trying to launch an AsyncTask and found that the reason it would fail was that an unrelated thread would throw an unhanded  exception early on. It seems the AsyncTask and the other thread (used to fetch ad banners) shared resources and when the ad thread misbehaved it made the class loader misbehave as well.Managed to figure this out while reading your post after obsessing for 4 days. Thank you!------=_Part_5884_488802.1314639854427Content-Type: text/html; charset=UTF-8Content-Transfer-Encoding: 7bitWas faced with the same error when trying to launch an AsyncTask and found that the reason it would fail was that an unrelated thread would throw an&nbsp;unhanded&nbsp; exception early on. It seems the AsyncTask and the other thread (used to fetch ad banners) shared resources and when the ad thread misbehaved it made the class loader misbehave as well.<div><br></div><div>Managed to figure this out while reading your post after obsessing for 4 days. Thank you!</div>------=_Part_5884_488802.1314639854427--------=_Part_5883_18950387.1314639854427--

上面的东西还能大致看懂:由于其他线程和AsyncTask在装载时都在竞争相同的资源,导致AsyncTask竞争失败,进一步导致class loader装载它失败。

怎么会这样呢, 想不通?!

查看源代码http://www.oschina.net/code/explore/android-2.2-froyo/android/os/AsyncTask.java

Line 184:Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

再看: http://www.oschina.net/code/explore/android-2.2-froyo/android/os/Process.java

Line 126:publicstaticfinalintTHREAD_PRIORITY_LOWEST = 19;

Line 136:publicstaticfinalintTHREAD_PRIORITY_BACKGROUND = 10;

看完后真的感到无可奈何! 调整线程优先级看来没辙了。

继续奇想: 让class loader优先装载它如何?! 了解过class loader就知道(Class.forName): 如果一个class被装载过一次,以后就不会再出先类似:java.lang.NoClassDefFoundError.

到此,问题就解决了。 直接在Application#onCreate里靠前面的位置执行一个空的AsyncTask实例就解决问题。

如果你能确保你自己的线程顺序,把AsyncTask放在当前Activity里的其他线程之前也可以。但是没有上面的方法保险。

终于松了口气! 也真明白了线程优先级的危害。


更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. Android调用webservice接口
  3. Android-Universal-Image-Loader 源码解读
  4. Android面试基础题总结二
  5. Android视图加载流程(3)之ViewRootImpl的UI刷新机制
  6. 线程-进程间通信(操作系统、java、android)最全总结!
  7. Android(安卓)进程管理概述
  8. Android之Handler与线程
  9. Android-广播接收者;短信监听

随机推荐

  1. Android下载速度计算
  2. layout居中
  3. [置顶] android canvas 绘图笔记
  4. Android 无线连接及连接状态研究
  5. Android消息推送接收后,通知栏的显示
  6. Android下如何获得通话记录和短信记录
  7. Android SharedPreferences的工具类 - 简
  8. Emergency Call 分析 (Android 4.4 R1)
  9. 转android项目开发 工作日志 2011.10.8--
  10. android 分享到QQ空间的所有操作