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. publicclassDispTheadNameextendsThread{
7. publicvoidrun(){
8. System.out.println(this.getName());
9. }
10. publicstaticvoidmain(String[]args){
11. System.out.println(Thread.currentThread().getName());
12. DispTheadName thread1=newDispTheadName();
13. DispTheadName thread2=newDispTheadName();
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. publicclassChangeTheadNameextendsThread{
    7. privateStringwho;
    8. 
    9. publicvoidrun(){
    10. System.out.println(who+":"+this.getName());
    11. }
    12. publicChangeTheadName(Stringwho){
    13. super();
    14. this.who=who;
    15. }
    16. publicChangeTheadName(Stringwho,Stringname){
    17. super(name);
    18. this.who=who;
    19. }
    20. publicstaticvoidmain(String[]args){
    21. ChangeTheadName thread1=newChangeTheadName("thread1","MyThread1");//初始化线程名称
    22. ChangeTheadName thread2=newChangeTheadName("thread2");
    23. ChangeTheadName thread3=newChangeTheadName("thread3");
    24. thread2.setName("MyThread2");//调用线程类的setName()修改名称
    25. thread1.start();
    26. thread2.start();
    27. thread3.start();
    28. }
    29. }


    运行结果


      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. publicclassRunnableClassimplementsRunnable
      7. {
      8. publicvoidrun()
      9. {
      10. System.out.println(Thread.currentThread().getName());
      11. }
      12. publicstaticvoidmain(String[]args)
      13. {
      14. RunnableClass t1=newRunnableClass();
      15. RunnableClass t2=newRunnableClass();
      16. Threadthread1=newThread(t1,"MyThread1");
      17. Threadthread2=newThread(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. publicclassTheadStateextendsThread
      7. {
      8. publicvoidrun()
      9. {
      10. intn=0;
      11. while((n)<1000);
      12. }
      13. 
      14. publicstaticvoidmain(String[]args)throwsException
      15. {
      16. TheadState thread1=newTheadState();
      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. }
       }


      运行结果


      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. publicclassThreadFlagextendsThread
      7. {
      8. publicvolatilebooleanexit=false;
      9. 
      10. publicvoidrun()
      11. {
      12. while(!exit)
      13. System.out.println("运行!");
      14. }
      15. publicstaticvoidmain(String[]args)throwsException
      16. {
      17. ThreadFlagthread=newThreadFlag();
      18. thread.start();
      19. sleep(100);// 主线程延迟
      20. thread.exit=true;// 终止线程thread
      21. System.out.println("线程退出!");
      22. }
      23. }


      运行结果必定为


      1. 运行!
      2. 运行!
      3. 运行!
      4. 运行!
      5. 运行!
      6. 线程退出!



      参考文献


      1.java线程指南. http://java.chinaitlab.com/Special/javaline/Index.html


      2.线程的生命周期. http://java.chinaitlab.com/line/778850_2.html


      3.Java Thread suspend Example. http://www.codingdiary.com/developers/developers/diary/javaapi/java/lang/SampleCode/suspendExampleCode.html