在java中 创建一个线程可以有两种方法:1:实现Runnable接口 2:派生Thread类本身
1 实现Runnable接口
实现Runnable接口类只需要一个run()方法 run方法是程序中的另一个执行线程的入口点,这个线程在run方法返回时结束。
构造函数Thread(Runnable threadOb, String threadName)中的threadOb定义了线程的执行从哪开始。线程的名字由threadName决定。新线程创建之后,只有调用了他的start()方法才执行,本质上,start()方法执行对run()方法的调用。
例子:
ThreadDemo:
- package thread;
- public class ThreadDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new NewThread();
- try {
- for (int n = 5; n > 0; n--) {
- System.out.println("Main Thread:" + n);
- Thread.sleep(5000);
- }
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println("Main interrupted");
- }
- }
- }
NewThread:
- package thread;
- public class NewThread implements Runnable{
- Thread t;
- NewThread(){
- t = new Thread(this,"Demo Thread");
- //t = new Thread();
- System.out.println("this:"+this);
- System.out.println("Child Thread:"+t);
- t.start();
- }
- public void run()
- {
- try {
- for (int n = 5; n > 0; n--) {
- System.out.println("Child Thread:" + n);
- Thread.sleep(1000);
- }
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println("Child interrupted");
- }
- System.out.println("Exiting child thread");
- }
- }
运行结果:
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:
- package thread;
- public class NewThread1 extends Thread{
- NewThread1()
- {
- super("Demo Thread");
- System.out.println("Child thread:"+this);
- start();
- }
- public void run()
- {
- try {
- for (int n = 5; n > 0; n--) {
- System.out.println("Child Thread:" + n);
- Thread.sleep(500);
- }
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println("Child interrupted");
- }
- System.out.println("Exiting child thread");
- }
- }
super("Demo Thread");super方法的调用,它调用的线程构造函数是:public Thread(String threadname)
ExtendThread:
- package thread;
- public class ExtendThread {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new NewThread1();
- try {
- for (int n = 5; n > 0; n--) {
- System.out.println("Main Thread:" + n);
- Thread.sleep(1000);
- }
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println("Main interrupted");
- }
- System.out.println("Exiting Main thread");
- }
- }
运行结果输出:
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接口吧。
阅读(939) | 评论(0) | 转发(0) |