java线程技术2_线程的创建运行终止

来源:互联网 发布:我是皇进阶数据 编辑:程序博客网 时间:2024/05/02 00:01
1.创建和运行线程
在Java中,多线程的实现有两种方式:
      扩展java.lang.Thread类
      实现java.lang.Runnable接口

(1)扩展Thread类
      Thread Test = new Thread();
      Test.start();

(2)实现Runnable接口
将实现Runnable接口的类实例化      
      Test impelements Runnable;//继承接口,实现run()
 建立一个Thread对象,并将第一步实例化后的对象作为参数传入Thread类的构造方法。
      Test t = new Test();
      Thread test = new Thread(t);
最后通过Thread类的start方法建立线程。
      test.start();

建议使用runable实现java多线程,不管如何,最终都需要通过thread.start()来使线程处于可运行状态。

2.例:显示线程名称
  1. /**
  2.  * 运行二个线程,显示线程的默认名称.
  3.  * @version V1.0 ,2011-3-24
  4.  * @author xiahui
  5.  */
  6. public class DispTheadName extends Thread {
  7.     public void run() {
  8.         System.out.println(this.getName());
  9.     }
  10.     public static void main(String[] args) {
  11.         System.out.println(Thread.currentThread().getName());
  12.         DispTheadName thread1 = new DispTheadName();
  13.         DispTheadName thread2 = new DispTheadName();
  14.         thread1.start();
  15.         thread2.start();
  16.     }
  17. }
显示结果
  1. main
  2. Thread-1
  3. Thread-0
main是主线程的名子,Thread-1和Thread-2分别是thread1和thread2的输出结果。

3.例:修改线程名称
  1. /**
  2.  * 修改线程的名称
  3.  * @version V1.0 ,2011-3-24
  4.  * @author xiahui
  5.  */
  6. public class ChangeTheadName extends Thread {
  7.     private String who;

  8.     public void run() {
  9.         System.out.println(who + ":" + this.getName());
  10.     }
  11.     public ChangeTheadName(String who) {
  12.         super();
  13.         this.who = who;
  14.     }
  15.     public ChangeTheadName(String who, String name) {
  16.         super(name);
  17.         this.who = who;
  18.     }
  19.     public static void main(String[] args) {
  20.         ChangeTheadName thread1 = new ChangeTheadName("thread1", "MyThread1");//初始化线程名称
  21.         ChangeTheadName thread2 = new ChangeTheadName("thread2");
  22.         ChangeTheadName thread3 = new ChangeTheadName("thread3");
  23.         thread2.setName("MyThread2");//调用线程类的setName()修改名称
  24.         thread1.start();
  25.         thread2.start();
  26.         thread3.start();
  27.     }
  28. }
运行结果
  1. thread2:MyThread2
  2. thread1:MyThread1
  3. thread3:Thread-1
注意:
      在调用start方法前后都可以使用setName设置线程名,但在调用start方法后使用setName修改线程名,会产生不确定性,也就是说可能在 run方法执行完后才会执行setName.如果在run方法中要使用线程名,就会出现虽然调用了setName方法,但线程名却未修改的现象。

      Thread类的start方法不能多次调用,如不能调用两次thread1.start()方法。否则会抛出一个IllegalThreadStateException异常。

4.通过Runnable接口来创建线程
  1. /**
  2.  * create thread by Runnable Interface
  3.  * @version V1.0 ,2011-3-27 
  4.  * @author xiahui
  5.  */
  6. public class RunnableClass implements Runnable
  7. {
  8.     public void run()
  9.     {
  10.         System.out.println(Thread.currentThread().getName());
  11.     }
  12.     public static void main(String[] args)
  13.     {
  14.         RunnableClass t1 = new RunnableClass();
  15.         RunnableClass t2 = new RunnableClass();
  16.         Thread thread1 = new Thread(t1, "MyThread1");
  17.         Thread thread2 = new Thread(t2);
  18.         thread2.setName("MyThread2");
  19.         thread1.start();
  20.         thread2.start();
  21.     }
  22. }
运行结果
  1. MyThread1
  2. MyThread2

5.线程的运行

      线程在建立后并不马上执行run方法中的代码,而是处于等待状态。线程处于等待状态时,可以通过Thread类的方法来设置线程不各种属性,如线程的优先级(setPriority)、线程名(setName)和线程的类型(setDaemon)等。
      当调用start方法后,线程开始执行run( )中的代码。线程进入运行状态。可以通过Thread类的isAlive方法来判断线程是否处于运行状态。
      当线程处于运行状态时,isAlive返回true,当isAlive返回false时,可能线程处于等待状态,也可能处于停止状态。

6 例:线程的创建、运行和停止
  1. /**
  2.  * 线程的创建、运行和停止.
  3.  * @version V1.0 ,2011-3-27 
  4.  * @author xiahui
  5.  */
  6. public class TheadState extends Thread
  7. {
  8.     public void run()
  9.     {
  10.         int n = 0;
  11.         while (( n) < 1000); 
  12.     }
  13.      
  14.     public static void main(String[] args) throws Exception
  15.     {
  16.         TheadState thread1 = new TheadState();
  17.         System.out.println("isAlive: " thread1.isAlive());
  18.         thread1.start();
  19.         System.out.println("isAlive: " thread1.isAlive());
  20.         thread1.join(); // 等线程thread1结束后再继续执行 
  21.         System.out.println("thread1已经结束!");
  22.         System.out.println("isAlive: " thread1.isAlive());
  23.     }
  24. }
运行结果
  1. isAlive: false
  2. isAlive: true
  3. thread1已经结束!
  4. isAlive: false
7.终止线程
    有三种方法可以使终止线程
    1.  使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
    2.  使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,就象突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果)。
    3.  使用interrupt方法中断线程。
         使用interrupt方法来终端线程可分为两种情况:
        (1)线程处于阻塞状态,如使用了sleep方法。
        (2)使用while(!isInterrupted()){……}来判断线程是否被中断。
        在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出

8. 例:通过退出标志终止线程
  1. /**
  2.  * 通过退出标志终止线程.
  3.  * @version V1.0 ,2011-3-29 
  4.  * @author xiahui
  5.  */
  6. public class ThreadFlag extends Thread
  7. {
  8.     public volatile boolean exit = false;

  9.     public void run()
  10.     {
  11.         while (!exit)
  12.             System.out.println("运行!");
  13.     }
  14.     public static void main(String[] args) throws Exception
  15.     {
  16.         ThreadFlag thread = new ThreadFlag();
  17.         thread.start();
  18.         sleep(100); // 主线程延迟
  19.         thread.exit = true; // 终止线程thread
  20.         System.out.println("线程退出!");
  21.     }
  22. }
运行结果必定为
  1. 运行!
  2. 运行!
  3. 运行!
  4. 运行!
  5. 运行!
  6. 线程退出!