watchthread

useful if we need to watch the status of a thread.

import android.os.Message;
import android.os.Handler;
import android.os.MessageQueue;
import android.os.Looper;
import java.util.ArrayList;
import android.util.Log;

public class WatcherThread extends Thread{


    private ArrayList<ThreadObservableInfo> mObservableList;
    boolean bRun;
    public String TAG = "WatcherThread";
    public static WatcherThread g_instance = null;

    private WatcherThread(){
        bRun = true;
        mObservableList 
        = new ArrayList<ThreadObservableInfo>();
    }

    public static WatcherThread getInstance(){
        if(null == g_instance){
            g_instance = new WatcherThread();
        }
        return g_instance;
    }

    boolean addCurrent(){
        return addObservable(CurrentThreadInfo());
    }
    boolean  addObservable(ThreadObservableInfo threadInfo){
        for (ThreadObservableInfo info : mObservableList) {
           if (info.mThread.getId()== Thread.currentThread().getId()){
               return false;
           }
        }
        return mObservableList.add(threadInfo);
    }
    boolean  removeCurrent(){
        for (ThreadObservableInfo info : mObservableList) {
           if (info.mThread.getId()== Thread.currentThread().getId()){
               return removeObserverable(info);
           }
        }
        return false;
    }
    boolean  removeObserverable(ThreadObservableInfo threadInfo){
        return mObservableList.remove(threadInfo);
    }
    void clear(){
        mObservableList.clear();
    }

    void startTrace(){
        synchronized(WatcherThread.this){
            bRun = true;
        }
        try{
            this.start();
        }catch(Exception e){
            Log.d(TAG, "lyftest new e="+e);
        }
    }
    
    
    @Override
    public void run(){
        while(bRun){
            
            for (ThreadObservableInfo info : mObservableList) {
               info.setAlive(false);
               info.helloObserver();
            }
            try{
                Thread.sleep(800);
            }catch(InterruptedException e){
                ;
            }
            for (ThreadObservableInfo info : mObservableList) {
               info.checkAlive();
            }
        }
    }
    public void stopTrace(){
        synchronized(WatcherThread.this){
            bRun = false;
        }
        g_instance = null;
    }

    public ThreadObservableInfo CurrentThreadInfo(){
        return new ThreadObservableInfo();
    }
    
    public class ThreadObservableInfo{
        public MessageQueue mQueue;
        public Handler mHandler;
        public Thread mThread;
        public boolean mAlive = false;
        public String mName;
        public int mTimes = 0;
        ThreadObservableInfo(){
            //get  infor from the current thread;
            mHandler = new Handler();
            mQueue = Looper.getMainLooper().myQueue();
            mThread = Thread.currentThread();
        }
        
        void setAlive(boolean value){
            mAlive = value;
        }
        void helloObserver(){
            Runnable r =  new Runnable(){
                 public void run(){
                     setAlive(true);
                 }
             };
             Message msg = Message.obtain(mHandler, r);
             mHandler.sendMessage(msg);
        }
        boolean isAlive(){
            return mAlive;
        }
        void dumpThread(){
            Log.d("ThreadObservableInfo", "WatcherThread name="+mName);
            mQueue.dumpQueue_l();
        }
        void checkAlive(){
            if (mAlive){
                mTimes = 0;
                return;
            }
            if (mTimes >= 5){
                mTimes = 0;
                dumpThread();
            }
            
        }
    }

    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值