Chinaunix首页 | 论坛 | 博客
  • 博客访问: 617718
  • 博文数量: 72
  • 博客积分: 1177
  • 博客等级: 少尉
  • 技术积分: 856
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-23 23:03
文章分类

全部博文(72)

文章存档

2015年(13)

2014年(5)

2013年(7)

2012年(39)

2011年(8)

分类: Java

2012-07-31 21:58:49

在java中 创建一个线程可以有两种方法:1:实现Runnable接口 2:派生Thread类本身

1 实现Runnable接口

   实现Runnable接口类只需要一个run()方法 run方法是程序中的另一个执行线程的入口点,这个线程在run方法返回时结束。
   构造函数Thread(Runnable threadOb, String threadName)中的threadOb定义了线程的执行从哪开始。线程的名字由threadName决定。新线程创建之后,只有调用了他的start()方法才执行,本质上,start()方法执行对run()方法的调用。

例子:
ThreadDemo:

点击(此处)折叠或打开

  1. package thread;

  2. public class ThreadDemo {

  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub

  8.         new NewThread();
  9.         try {
  10.             for (int n = 5; n > 0; n--) {
  11.                 System.out.println("Main Thread:" + n);
  12.                 Thread.sleep(5000);
  13.             }
  14.         } catch (Exception e) {
  15.             // TODO: handle exception
  16.             System.out.println("Main interrupted");
  17.         }
  18.     }

  19. }
NewThread:

点击(此处)折叠或打开

  1. package thread;

  2. public class NewThread implements Runnable{
  3.     Thread t;
  4.     NewThread(){
  5.         t = new Thread(this,"Demo Thread");
  6.         //t = new Thread();
  7.         System.out.println("this:"+this);
  8.         System.out.println("Child Thread:"+t);
  9.         t.start();
  10.     }
  11.     public void run()
  12.     {
  13.         try {
  14.             for (int n = 5; n > 0; n--) {
  15.                 System.out.println("Child Thread:" + n);
  16.                 Thread.sleep(1000);
  17.             }
  18.         } catch (Exception e) {
  19.             // TODO: handle exception
  20.             System.out.println("Child interrupted");
  21.         }
  22.         System.out.println("Exiting child thread");
  23.     }

  24. }

运行结果:
Child Thread:Thread[Demo Thread,5,main]
Main Thread:5
Child Thread:5
Child Thread:4
Child Thread:3
Main Thread:4
Child Thread:2
Main Thread:3
Child Thread:1
Exiting child thread
Main Thread:2
Main Thread:1


在上诉代码中, 如果将NewThread中的t = new Thread(this,"Demo Thread");改为t = new Thread();则子线程将不会执行,结果如下:
this:thread.NewThread@c17164
Child Thread:Thread[Thread-0,5,main]
Main Thread:5
Main Thread:4
Main Thread:3
Main Thread:2
Main Thread:1



将t.start()改为t.run()。执行效果如下:
this:thread.NewThread@c17164
Child Thread:Thread[Demo Thread,5,main]
Child Thread:5
Child Thread:4
Child Thread:3
Child Thread:2
Child Thread:1
Exiting child thread
Main Thread:5
Main Thread:4
Main Thread:3
Main Thread:2
Main Thread:1

可以看出t = new Thread(this,"Demo Thread") 第一个参数this表示希望新线程调用this对象上的run方法,然后再调用start(),也就是从run()方法开始启用线程的执行。调用start后,NewThread的构造函数返回到main函数中,主线程重新激活并进入for循环。

2 派生Thread类本身,扩展线程

  第二种创建线程的方法是创建一个扩展Thread类的新类,然后创建了一个该类的实例。这个扩展类必须重写run()方法,这是新线程的进入点,同时也必须调用start()方法来执行新线程。

NewThread1:

点击(此处)折叠或打开

  1. package thread;

  2. public class NewThread1 extends Thread{

  3.     NewThread1()
  4.     {
  5.         super("Demo Thread");
  6.         System.out.println("Child thread:"+this);
  7.         start();
  8.     }
  9.     public void run()
  10.     {
  11.         try {
  12.             for (int n = 5; n > 0; n--) {
  13.                 System.out.println("Child Thread:" + n);
  14.                 Thread.sleep(500);
  15.             }
  16.         } catch (Exception e) {
  17.             // TODO: handle exception
  18.             System.out.println("Child interrupted");
  19.         }
  20.         System.out.println("Exiting child thread");
  21.     }

  22. }
  super("Demo Thread");super方法的调用,它调用的线程构造函数是:public Thread(String threadname)

ExtendThread:

点击(此处)折叠或打开

  1. package thread;

  2. public class ExtendThread {

  3.     public static void main(String[] args) {
  4.         // TODO Auto-generated method stub

  5.         new NewThread1();
  6.         try {
  7.             for (int n = 5; n > 0; n--) {
  8.                 System.out.println("Main Thread:" + n);
  9.                 Thread.sleep(1000);
  10.             }
  11.         } catch (Exception e) {
  12.             // TODO: handle exception
  13.             System.out.println("Main interrupted");
  14.         }
  15.         System.out.println("Exiting Main thread");
  16.     }

  17. }
运行结果输出:
Child thread:Thread[Demo Thread,5,main]
Main Thread:5
Child Thread:5
Child Thread:4
Child Thread:3
Main Thread:4
Child Thread:2
Main Thread:3
Child Thread:1
Exiting child thread
Main Thread:2
Main Thread:1
Exiting Main thread



两种创建子线程的方法,如果需要以某种方式增强或修改Thread类时才应该派生Thread,如果不想重写Thread类的其他方法,还是简单的实现Runnable接口吧。

阅读(1438) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~