1

设置: 我有服务类和绑定到它的两个活动。第一个是调用的主要 UI 活动

startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE).  

第二个活动只调用 bindService(intent, mConnection, Context.BIND_AUTO_CREATE)。

两者都在 onStop() 中调用 unbindService(mConnection)。

主 UI 上有一个名为“按钮 A”的按钮,按下该按钮时会将 arrayList 传递给服务上的方法。然后服务中的方法启动一个新的静态线程来处理这个arrayList。新线程还有一个静态消息处理程序来与服务中的方法进行通信。第二个活动调用服务执行计算,然后将结果保存在数据库中。

我只在服务中明确声明了一个线程。所以我希望总共有 2 个线程,包括主线程,我只希望有两个绑定器,因为我只有两个绑定到服务的活动。但是,当我在主 UI 的 onCreate 中执行 Thread.activeCount() 时,我最初得到 3 个线程。后来我得到 4 个线程,然后是 9 个线程和 11 个线程,我按照下面描述的方式进行操作。

这是问题。
如何将活页夹和异步任务添加到我的线程组列表中? 他们为什么不断扩张?我担心电池消耗,更多线程是否意味着更多电池消耗?或者它只是工作更快,这些自动生成的线程会自己收集垃圾?我有任何内存泄漏吗?我可以控制这些线程是如何产生的吗?

我没有找到太多关于此的文档。任何人都了解这个问题,请告诉我。

应用程序的首次运行,在主 UI 的 onCreate 中:

ThreadGroup ctg = Thread.currentThread().getThreadGroup(); ctg.list();

该列表显示:

I/System.out(18606):     Thread[main,5,main]
I/System.out(18606):     Thread[Thread-5,5,main]
I/System.out(18606):     Thread[Binder_1,5,main]
I/System.out(18606):     Thread[Binder_2,5,main]

我知道 main、Binder_1、Binder_2 是 3 个活动线程。 我重新定位我的手机,logcat 显示:

MainActivity   onStop unbindService
MainActivity   onDestroy() plus debuginfo --Pid is 18606, threadid is 18606, there are 4 threads
MainActivity   onCreate() plus debuginfo --Pid is 18606, threadid is 18606, there are 4 threads
I/System.out(18606): java.lang.ThreadGroup[name=main,maxPriority=10]
I/System.out(18606):     Thread[main,5,main]
I/System.out(18606):     Thread[Thread-5,5,main]
I/System.out(18606):     Thread[Binder_1,5,main]
I/System.out(18606):     Thread[Binder_2,5,main]
I/System.out(18606):     Thread[Binder_3,5,main]
Service   onStartCommand() 2--Pid is 18606, threadid is 18606, there are 4 threads

然后我在主 UI 上按下“按钮 A”,通过创建一个新线程来处理 arraylist,然后我得到 11 个线程。 Logcat 显示:

D/Service(18606): in processRuleOnArrayList -- ACTUALLY MADE A NEW THREAD
D/BoundService(18606): processRuleOnArrayList(): --Pid is 18606, process threadid is 18606, service     threadid is 2930, service threadname is serviceName_0, there are 11 threads
D/Service(18606): processRuleOnArrayList(): service thread's threadGroup main, main thread's threadGroup java.lang.ThreadGroup[name=main,maxPriority=10]
I/System.out(18606): java.lang.ThreadGroup[name=main,maxPriority=10]
I/System.out(18606):     Thread[main,5,main]
I/System.out(18606):     Thread[Thread-5,5,main]
I/System.out(18606):     Thread[Binder_1,5,main]
I/System.out(18606):     Thread[Binder_2,5,main]
I/System.out(18606):     Thread[Binder_3,5,main]
I/System.out(18606):     Thread[AsyncTask #1,5,main]
I/System.out(18606):     Thread[AsyncTask #2,5,main]
I/System.out(18606):     Thread[AsyncTask #3,5,main]
I/System.out(18606):     Thread[AsyncTask #4,5,main]
I/System.out(18606):     Thread[AsyncTask #5,5,main]
I/System.out(18606):     Thread[Binder_4,5,main]
I/System.out(18606):     Thread[serviceName_0,5,main]
4

1 回答 1

0

您应该保留对您创建的 AsyncTask 和服务绑定的引用并重用它们(或者如果需要重新创建它们,则将它们销毁)。

请参阅http://developer.android.com/reference/android/app/Activity.html并搜索 onSaveInstanceState。

于 2014-07-16T07:14:28.517 回答