How to properly stop the Thread in Java?

后端 未结 9 1033
执念已碎
执念已碎 2020-11-21 16:19

I need a solution to properly stop the thread in Java.

I have IndexProcessorclass which implements the Runnable interface:

public class          


        
9条回答
  •  温柔的废话
    2020-11-21 16:21

    Sometime I will try 1000 times in my onDestroy()/contextDestroyed()

          @Override
        protected void onDestroy() {
            boolean retry = true;
            int counter = 0;
            while(retry && counter<1000)
            {
                counter++;
                try{thread.setRunnung(false);
                    thread.join();
                    retry = false;
                    thread = null; //garbage can coll
                }catch(InterruptedException e){e.printStackTrace();}
            }
    
        }
    

提交回复
热议问题