0

First time ,i invoke HandlerThread.start() to handle the background service. after all the stuffs completed,I wanna to end this Thread by calling HandlerThread.quit().

Then the second time,I start this Handler,and checked the HandlerThread.isAlive(),the isAlive() return false, but when i invoke HandlerThread again by HandlerThread.start().

But I got the IllegalThreadStateException,why?

How can i really stop the HandlerThread before I invoke handlerThread.start() again safely?

  onCreate(){
  ...............
  CurrentLocationPresenter = 
       new  CurrentLocationPresenter(getApplicationContext(),mHandler);
    }

  public void onClick(View v) {
    int id = v.getId();
    switch (id){
        case  R.id.showplacebutton:
            showPlaceInMapActivity();
            break;

        case  R.id.showgpsbutton:     

            if (mCurrentLocationPresenter.isAlive()){
                break;
            }

            mCurrentLocationPresenter.start();
            break;


    private Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        if (msg.what == CurrentLocationPresenter.WHATCODE){

            mCurrentLatlng = (LatLng) msg.obj;
            mTextView.setVisibility(View.VISIBLE);
            if (mCurrentLatlng!=null) {
                mTextView.setText(mCurrentLatlng.toString());
            }
            mCurrentLocationPresenter.getLooper().quit();
        }
    }
};
Lee
  • 25
  • 10

2 Answers2

0

You can't call asyntask.execute() on same object more than one.

Always call MyasynTask asyntask=new MyasynTask();

 asyntask.execute();

for more enter link description here

Community
  • 1
  • 1
Rahul Chaudhary
  • 1,059
  • 1
  • 6
  • 15
  • First time,I found this phenomenon.But I thought this will create a new object everytime when I Click this button. Would this waste too many memory? – Lee Aug 05 '15 at 14:19
0

As stated, you can never call the start/run/execute method on a thread object more than once, as you will get the IllegalThreadStateException.

You can, however, use something such as an ExecutorService which will allow you to use the same Runnable multiple times.

Also, if you use a ThreadPoolExecutor, which is a descendant of ExecutorService, the memory and thread management is taken care of.

  • thanks ,i have already know this by read the souce code of google ,because after this thread.start() ,the thread has been marked,and can not being start again. – Lee Aug 05 '15 at 16:11
  • I also don't see why you would want to stop the thread, as the HandlerThread's looper will continue to handle messages until it is told to stop. Unless that is your intention, in which case, I have to ask why are you trying to stop and then restart the looper? –  Aug 05 '15 at 16:20
  • If you want to stop just the looper (the part where the messages actually get handled) of the thread, then you'd call mCurrentLocationPresenter.getLooper().stop(); And to restart the looper mCurrentLocationPresenter.getLooper().loop(); From what I am understanding, you don't need to restart the thread, you need to restart the looper in the thread. –  Aug 05 '15 at 16:33
  • Thank you again.The case like this:when I Click the Button then I start a HandlerThread to send message to the UI Thread,but if I want to Click this Button again,the app crashed.That's my case.Now I set the Button unClickable after first time,or every time I clicking the button then new this HandlerThread. – Lee Aug 06 '15 at 01:16