如何结束一个线程?
关于线程的结束有以下几点:
1.不要手动调用stop方法强行终止一个线程,这种方式不安全。
通过帮助文档,我们可以知道,Android的线程类本身就提供了一些公共方法去结束线程。
final void stop()
This method is deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state
但是,通过说明我们可以看到,这些方法Android本身都是不推荐使用的,通过这种方式结束线程是不安全的。
2.线程里run函数短,执行完后线程会自行销毁,不用手动去终止。
3.手动停止,通过在run里设置标志先停止运行,再调用Thread.interrupt();注意,在run没有停止时调用.interrupt()没有效果。
1. 自己加入一个成员变量, 我们在程序的循环里面, 轮流的去检查这个变量, 变量变化时,就会退出这个线程. 代码示例如下
-
- package com.test;
-
- public class StopThread extends Thread {
-
- private boolean _run = true;
- public void stopThread( boolean run) {
- this ._run = !run;
- }
-
- @Override
- public void run() {
- while(_run) {
-
- //[img]http://www.blogjava.net/Images/dot.gif[/img]数据处理
那么阻塞的语句,怎么终止线程呢?
Java虽然deprecate了一个stop,但是,提供了interrupt(),这个方法是安全的. 这个中断方法可以将阻塞的线程唤醒过来, 但是注意 他不能将非阻塞的线程中断. 中断的同时,会抛出一个异常InterruptedException. 幸运的是, SocketChannel.connect() .read() 阻塞方法都会接受中断,ClosedByInterruptException.
这时我们不轮询变量了, 轮询当前线程是否被中断, 代码
-
- System.out.println("start");
- while(!this.isInterrupted()) {
- [img]http://www.blogjava.net/Images/dot.gif[/img]数据处理
-
-
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println(stop);
- super.run();
- }
-
- public static void main(String[] args) {
- StopThread thread=new StopThread();
- thread.start();
- try {
- Thread.sleep(1000);
- }catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- thread.interrupt();
- System.out.println(interrupt);
- }
- }
final static void killProcess(int pid) 其中,pid, 可以通过Process.mypid() 获取, 但这样终结的是整个程序, 不是我们所想要的.
==================================================分割线==========================================
如果该线程处在不可中断状态下,就是没有调用上述api,那么java只是设置一下该线程的interrupt状态,其他事情都不会发生,如果该线程之后会调用行数阻塞API,那到时候线程会马会上跳出,并抛出InterruptedException,接下来的事情就跟第一种状况一致了。如果不会调用阻塞 API,那么这个线程就会一直执行下去。除非你就是要实现这样的线程,一般高性能的代码中肯定会有wait(),yield()之类出让cpu的函数,不会发生后者的情况。
-
- readCacheThread = new Thread(){
- public void run() {
-
- try {
- Method getPackageSizeInfo = pm.getClass().getMethod(
- "getPackageSizeInfo", String.class,
- IPackageStatsObserver.class);
- for (AppInfoItem item : installedApp) {//个人应用
- sleep(1);//interrupt后会抛异常,这样就可以提前结束线程
- getPackageSizeInfo.invoke(pm, item.packageName, pkgsizeobserver);
- }
- for (AppInfoItem item : systemApp) {//系统应用
- sleep(1);
- getPackageSizeInfo.invoke(pm, item.packageName, pkgsizeobserver);
- }
-
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- Log.e("qqqqqqqqqqqqq", "sleep over");
- return;
- }
-
- };
- };
- readCacheThread.start();
-
- if(readCacheThread != null && readCacheThread.isAlive()){
- //Log.e("readCacheThread", "thread interrupt_1");
- readCacheThread.interrupt();
- //Log.e("status", ""+readCacheThread.isInterrupted());
- }
这样便可提前退出一个线程。
版权声明:本文为博主原创文章,未经博主允许不得转载。