Java Thread Stop方法以及替换实现

来源:互联网 发布:身份鉴权超时cms 编辑:程序博客网 时间:2024/05/22 20:29

http://blog.csdn.net/longronglin/article/details/3408510

Stop方法不推荐使用,我给个具体的例子:

  1. public class DeprecatedStop extends Object implements Runnable {
  2.     public void run() {
  3.         int count = 0;
  4.         while ( count <20 ) {
  5.             System.out.println("Running ... count=" + count);
  6.             count++;
  7.             try {
  8.                 Thread.sleep(300);
  9.             } catch ( InterruptedException x ) {
  10.                 // ignores
  11.             }
  12.         }
  13.         
  14.         // the code maybe not executed 
  15.         System.out.println("stoped");
  16.     }
  17.     public static void main(String[] args) {
  18.         DeprecatedStop ds = new DeprecatedStop();
  19.         Thread t = new Thread(ds);
  20.         t.start();
  21.         try {
  22.             Thread.sleep(2000);
  23.         } catch ( InterruptedException x ) {
  24.             // ignore
  25.         }
  26.         // Abruptly stop the other thread in its tracks!
  27.         t.stop();
  28.     }
  29. }

 

 

可能的运行结果:

  1. Running ... count=0
  2. Running ... count=1
  3. Running ... count=2
  4. Running ... count=3
  5. Running ... count=4
  6. Running ... count=5
  7. Running ... count=6

可以发现程序中的打印stoped并没有执行,所以说如果在程序中有其他操作,如果线程突然stop是会带来严重的影响的。所以怎么也应该使用该操作。当然如果是我上面的程序代码突然stop的影响其实是没有的,但是如果是其他打开文件最后需要释放或者什么的就会带来严重的影响了。

 

如何在程序中对其进行停止呢?

 

  1. public class AlternateStop extends Object implements Runnable {
  2.     private volatile boolean stopRequested;
  3.     private Thread runThread;
  4.     public void run() {
  5.         runThread = Thread.currentThread();
  6.         stopRequested = false;
  7.         int count = 0;
  8.         while ( !stopRequested ) {
  9.             System.out.println("Running ... count=" + count);
  10.             count++;
  11.             try {
  12.                 Thread.sleep(300);
  13.             } catch ( InterruptedException x ) {
  14.                 Thread.currentThread().interrupt(); // re-assert interrupt
  15.             }
  16.         }
  17.         
  18.         System.out.println("stoped");
  19.     }
  20.     public void stopRequest() {
  21.         stopRequested = true;
  22.         if ( runThread != null ) {
  23.             runThread.interrupt();
  24.         }
  25.     }
  26.     public static void main(String[] args) {
  27.         AlternateStop as = new AlternateStop();
  28.         Thread t = new Thread(as);
  29.         t.start();
  30.         try {
  31.             Thread.sleep(2000);
  32.         } catch ( InterruptedException x ) {
  33.             // ignore
  34.         }
  35.         as.stopRequest();
  36.     }
  37. }

可能的运行结果如下:

  1. Running ... count=0
  2. Running ... count=1
  3. Running ... count=2
  4. Running ... count=3
  5. Running ... count=4
  6. Running ... count=5
  7. Running ... count=6
  8. stoped

这样我们就解决了强制 stop的问题。

0 0
原创粉丝点击