猿问

我怎么能杀死一根线?不用停止();

我怎么能杀死一根线?不用停止();

Thread currentThread=Thread.currentThread();
        public void run()
        {               

             while(!shutdown)
            {                               
                try
                {
                    System.out.println(currentThread.isAlive());
                Thread.interrupted();

                System.out.println(currentThread.isAlive());
                if(currentThread.isAlive()==false)
                {
                    shutdown=true;
                }
                }
                catch(Exception e)
                {
                    currentThread.interrupt();
                }                   
            }
        }

    });
    thread.start();


Smart猫小萌
浏览 418回答 3
3回答

慕姐4208626

通常,当线程的中断..那么,为什么不使用本机布尔?试一试isInterrupted():   Thread t = new Thread(new Runnable(){         @Override         public void run() {             while(!Thread.currentThread().isInterrupted()){                 // do stuff                      }            }});     t.start();     // Sleep a second, and then interrupt     try {         Thread.sleep(1000);     } catch (InterruptedException e) {}     t.interrupt();
随时随地看视频慕课网APP

相关分类

Java
我要回答