我有一个活动创建一个工作线程,用于转换图片列表中的图片。是的,在onPause()/ onResume()中,我启动并停止工作线程。跳过工作者的肉,我有一个循环,当它被中断并且主变量设置为null(或另一个工人)时结束:
public void run()
{
while(transitionWorkerThread == this)
{
try
{
Log.d(getLogTag(), "Ready to sleep.");
Thread.sleep(5000);
Log.d(getLogTag(), "ready to transition, is running? " + (transitionWorkerThread == this));
if(transitionWorkerThread == this)
{
Log.d(getLogTag(), "ready to post image change");
MAIN_THREAD_HANDLER.post(new Runnable()
{
@Override
public void run()
{
Log.d(getLogTag(), "ready to go to next image");
imageSlideView.goNextImage();
Log.d(getLogTag(), "supposedly went to next image");
}
});
Log.d(getLogTag(), "posted image change, is running? " + (transitionWorkerThread == this));
}
}
catch(final InterruptedException interruptedException)
{
Log.d(getLogTag(), "Worker interrupted.");
}
catch(final Throwable throwable)
{
Log.d(getLogTag(), "some sort of picture error?", throwable);
}
}
Log.d(getLogTag(), "dropped out of picture loop, worker: " + transitionWorkerThread + " is us? " + (transitionWorkerThread == this));
}
现在这就是谜团:线程消失了!它会在一段时间后停止执行!认真!看看这个日志输出:
11-22 13:13:40.186: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:13:40.191: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:40.191: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:45.253: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:13:45.253: D/PictureFrameActivity(941): ready to post image change
11-22 13:13:45.253: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:13:45.253: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:13:45.253: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:45.253: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:50.272: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:13:50.272: D/PictureFrameActivity(941): ready to post image change
11-22 13:13:50.272: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:50.272: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:50.301: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:13:50.301: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:13:55.325: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:13:55.325: D/PictureFrameActivity(941): ready to post image change
11-22 13:13:55.325: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:55.325: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:55.331: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:13:55.331: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:14:00.011: E/dalvikvm(76): No free temp registers
11-22 13:14:00.011: E/dalvikvm(76): Jit: aborting trace compilation, reverting to interpreter
11-22 13:14:00.365: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:14:00.365: D/PictureFrameActivity(941): ready to post image change
11-22 13:14:00.365: D/PictureFrameActivity(941): ready to go to next image
11-22 13:14:00.365: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:14:00.371: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:14:06.042: D/dalvikvm(941): GC_CONCURRENT freed 2106K, 58% free 3260K/7751K, external 13519K/13938K, paused 15ms+10ms
11-22 13:14:06.541: I/dalvikvm(941): Jit: resizing JitTable from 1024 to 2048
11-22 13:14:11.111: D/dalvikvm(941): GC_CONCURRENT freed 2051K, 57% free 3342K/7751K, external 13519K/13938K, paused 19ms+17ms
11-22 13:14:15.121: D/dalvikvm(941): GC_CONCURRENT freed 2174K, 59% free 3216K/7815K, external 13519K/13938K, paused 20ms+20ms
11-22 13:14:19.271: D/dalvikvm(941): GC_CONCURRENT freed 2028K, 59% free 3235K/7815K, external 13519K/13938K, paused 24ms+21ms
你会注意到在最后一次“发布图片更改后,正在运行?是真的”之后,线程就会消失,再也不会再听到了!看看代码,只有几个选择:我可以看到“工作人员中断”或“某种图片错误”甚至“退出图片循环”。如果一切顺利,它应该循环回来并说“准备好睡觉。”
但我从这个帖子中看不到任何其他内容!就像它在循环结束时被暂停一样。当然Android不会暂停一个帖子。这是一个谜。
更新:我已将整个工作线程取出并将其放入由活动调用的单独本地服务中。几个循环后,工作线程仍然消失。这很疯狂---工作线程是服务的常用用途。可疑的东西正在发生...但我仍然感谢有人给我一个关于它是什么的线索。
答案 0 :(得分:0)
我不确定为什么你的线程被杀死但我的猜测是它与活动生命周期有关。现在在Android中有一个很好的类,它允许简单的线程化,并像你的代码一样定期发布回UI线程。查看AsyncTask并使用publishProgress函数更新UI对象。我不确定你的代码有什么问题,但Android线程的最佳实践是使用这个类。
答案 1 :(得分:0)
尤里卡!这根本不是一个线程问题。这是我的错(惊喜!)。
我对原始代码并不完全真实。它实际上看起来像这样:
final long sleepLength = transitionIntervalManager.pollState();
try
{
Log.d(getLogTag(), "Ready to sleep for "+sleepLength);
Thread.sleep(sleepLength);
这个transitionIntervalManager
是一个奇特的类我写的,实际上减慢了过渡时间。我有一个错误,当它到达最后(最慢)的转换时间时,调用pollState
将进入无限循环并且永远不会返回。因为在第一个日志之前调用了该方法,所以日志永远不会被写入 - 而且我的线程仍然在试图进行下一次转换。 (难怪我的笔记本电脑风扇出现了!)
我试图简化代码,我没有意识到我正在简化问题 - 我的意思是,这么简单的代码怎么会有bug。 ;)