java吧 关注:1,274,291贴子:12,787,744
  • 6回复贴,共1

关于中断的线程如何继续。。在线等

只看楼主收藏回复

我用interrupt中断了一个线程,具体实现是在while (!this.currentThread().isInterrupted())。但是我想知道,怎么样才能继续这个进程。


IP属地:浙江1楼2011-12-08 16:15回复
    再次调用一遍.isInterrupted()可以吗?或者使用nofify()(不知道错没)唤醒一下!


    IP属地:辽宁2楼2011-12-08 16:27
    回复
      2025-08-17 06:31:44
      广告
      不感兴趣
      开通SVIP免广告



      IP属地:浙江3楼2011-12-08 16:34
      回复
        那个是我的run方法。
        我在另外一个类,new了MyThread,然后又interrupt(),还能继续吗?


        IP属地:浙江4楼2011-12-08 16:35
        回复
          notify() 试试吧


          IP属地:辽宁5楼2011-12-08 16:39
          回复
            虽然没成功,但还是谢谢你


            IP属地:浙江6楼2011-12-08 16:42
            回复
              看看这个例子
              public class TestThread implements Runnable{
              boolean stop = false;
              public static void main(String[] args) throws Exception {
              Thread thread = new Thread(new TestThread(),"My Thread");
              System.out.println( "Starting thread..." );
              thread.start();
              Thread.sleep( 3000 );
              System.out.println( "Interrupting thread..." );
              thread.interrupt();
              System.out.println("线程是否中断:" + thread.isInterrupted());
              Thread.sleep( 3000 );
              System.out.println("Stopping application..." );
              }
              public void run() {
              while(!stop){
              System.out.println( "My Thread is running..." );
              // 让该循环持续一段时间,使上面的话打印次数少点
              long time = System.currentTimeMillis();
              while((System.currentTimeMillis()-time < 1000)) {
              }
              }
              System.out.println("My Thread exiting under request..." );
              }
              }


              IP属地:江苏7楼2011-12-08 16:57
              回复