Chinaunix首页 | 论坛 | 博客
  • 博客访问: 518529
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2011-11-18 17:34:51

  1. /**
  2.  * 为啥不能使用 Thead.stop()? 那又该如何替换? 这里举例说明。
  3.  *
  4.  * 还真没细看过Thread的几个方法呢!
  5.  * Thread#join():父线程中调用子线程的join()方法,则父线程会被阻塞,直到子线程执行完毕。
  6.  * Thread.sleep():线程至少休眠一段时间,然后继续执行。
  7.  * Thread.yield():将当前返回等待执行的线程队列中,先执行其他要执行的线程。(貌似在进行优先级安排时可以用到)
  8.  * Thread#interrupt():调用该方法时,如果该线程正在运行,会将其标志为interrupted,此时如果调用
  9.  * Thread.currentThread().isInterrupted()将返回true。如果线程正在被block,比如(Sleep)
  10.  * 则会触发InterruptedException,但不会修改interrupted标志。
  11.  *
  12.  *
  13.  *
  14.  */
  15. public class TestThread {

  16.     public static void main(String[] args) throws InterruptedException {
  17.         // 错误的例子(打印了 "FINALLY SO",却未打印 "STOP S0")
  18.         if (args.length == 0) {
  19.             S0 s0 = new S0();
  20.             s0.start();
  21.             Thread.sleep(5000);
  22.             s0.stop(); // 直接终止线程,且之后的线程代码不会被执行。
  23.         } else {
  24.             // 推荐的例子(打印了"STOP S1")
  25.             S1 s1 = new S1();
  26.             s1.start();
  27.             Thread.sleep(5000);
  28.             s1.interrupt(); // 使用interrupt(),而非 stop();
  29.         }
  30.     }

  31.     public static class S0 extends Thread {
  32.         public void run() {
  33.             int i = 0;

  34.             int j = 1;
  35.             try {
  36.                 while (j == 1) {
  37.                     i++;
  38.                     System.out.println(i);
  39.                 }
  40.             }
  41.             // 如果有Catch 则会捕获到 ThreadDeath 这个ERROR。
  42.             // 但是它不是一个Exception,大家一般都不会写这样的catch语句的。
  43.             // 不能将线程的停止工作完全依赖于这种没有明确规则的编程感觉吧。
  44.             // catch (Throwable e) {
  45.             // System.out.println("CATCH : " + e.getClass()+" : " +
  46.             // e.getMessage());
  47.             // }

  48.             // 如果死循环代码中后有finally块,被stop()之后,该代码会被执行。
  49.             finally {
  50.                 System.out.println("FINALLY S0");
  51.             }

  52.             // 死循环代码之后的其他语句在被stop()之后将不会被执行。
  53.             System.out.println("STOPPED S0");// 这里如果是IO流的close() 会怎样?
  54.         }
  55.     }

  56.     // *************************************************************************
  57.     // 下面是推荐的例子:
  58.     // 1. 如果线程正在运行时被interrupted,则通过Thread.currentThread().isInterrupted()来终止运行。
  59.     // 2. 如果线程正在休眠时被interrupted,则只能通过捕获InterruptedException来终止欲行(不能通过方法一)。
  60.     // 注意:
  61.     // Thread.currentThread().isInterrupted()只会读取当前线程是否被终止。
  62.     // 而 Thread.interrupted() 则是先读取当前线程是否被终止,然后会将该被终止标志清除。
  63.     // *************************************************************************
  64.     public static class S1 extends Thread {

  65.         public void run() {

  66.             int i = 0;
  67.             try {
  68.                 while (!Thread.currentThread().isInterrupted()) {
  69.                     i++;
  70.                     System.out.println(i);

  71.                     Thread.sleep(1000);
  72.                 }
  73.             } catch (InterruptedException e) {
  74.                 // TODO
  75.             } finally {
  76.                 // 做清理工作
  77.             }
  78.             System.out.println("STOPPED S1 + "
  79.                     + Thread.currentThread().isInterrupted());
  80.         }
  81.         

  82.         // 如果觉得使用Thread.currentThread().isInterrupted()有些不好理解,
  83.         // 也可以while循环时判断自己定义的是否停止标志,如下所示。
  84.         // private boolean abort = false;
  85.         // public void abort(){
  86.         // abort = true;
  87.         // }
  88.     }
  89. }
阅读(2295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~