How to forcefully stop thread and start same thread with new variable?

后端 未结 4 2061
太阳男子
太阳男子 2020-12-12 03:58

I want to do some long running task on a button click, if user click again on that button current task forcefully stopped and will do next task ?

4条回答
  •  孤街浪徒
    2020-12-12 04:25

    You can not restart a thread once it is stopped.

    I would suggest to use Looper/Handler API to put the tasks in the queue and let the handler execute them on separate thread.

    I have demo project for looper/handler here with explanation. Have a look if you want to implement it this way.

    Another option (as mentioned by @Guna) is, you can crate cached pool of threads and let the executor run the tasks on threads.

提交回复
热议问题