I am building an application in which in an activity, I am using 2 threads,
1-main thread,
2-other thread
Now in some circumstances I want to force kill another thread,so that only my main thread should work..
currently I am using it as:
if(other_thread!=null)
other_thread.interrupt();
but in interrupt() method,when main thread completed its task,then other thread started doing its task..And my requirement is my application ends when main thread completed its task..So instead of using interrupt(),which steps I should follow to force kill this other_thread??
I won’t recommend you using the
interrupt()method or any other techniques of thread killing. If you have a thread with awhileloop inside, you can control this thread by a booleanflagfor thewhilecondition. When you set the flag tofalsethe thread just finishes its task. Then you can callthread.join()from your main thread to be sure that the other thread has finished execution. Hope this helps.Here’s a little example: