Chinaunix首页 | 论坛 | 博客
  • 博客访问: 274488
  • 博文数量: 138
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 971
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-03 10:05
文章分类

全部博文(138)

文章存档

2016年(1)

2015年(137)

我的朋友

分类: Android平台

2015-06-24 17:12:14

在android中开启的线程用Thread.stop()来关闭是不会真正关闭的,当我们再次start线程的时候,会产生异常:Thread is already started.

针对这个问题可以在线程的run方法里,加一个判断标志。例如:

              class TestThread extends Thread{

                         public void run(){

                                     while(isTrue){

                                               //做你要做的事。

                                     }

                         }

              }


运行完后 设置isTrue为false;并且将运行的线程对象挂起然后置为null(假设开启的线程对象为mTestThread) :

               mTestThread.interrupt();

               mTestThread=null;


阅读(725) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~