Java中线程的三种实现方式

来源:互联网 发布:周末父母 知乎 编辑:程序博客网 时间:2024/06/06 08:59


      Java中使用Thread代表线程类,所有的线程类都必须是Thread类或者其子类的实例。每个线程的作用就是完成一定的任务,即执行一段程序流。Java中使用线程执行体来表示这段程序流


Java中线程的实现方式有如下三种:

1.继承Thread类

public class Thread extends Object implements Runnable

定义Thread类的子类,并重写Thread类的run()方法,创建子类对象(即线程对象),调用线程对象的start()方法来启动该线程。eg:

public class ThreadDemo extends Thread {private int i;public void run(){for(; i<100 ;i++){System.out.println(getName() +" "+ i);}}public static void main(String[] args) {for(int i = 0 ;i<100; i++){                        //currentThread是Thread类的静态方法,该方法返回当前正在执行的线程对象                        //getName()返回当前线程对象的名字                        System.out.println(Thread.currentThread().getName()+" "+i);if(i==20){//启动两个线程,但是实际上有三个线程,即main主线程                                //用户启动的多个线程的名字依次为Thread-0、Thread-1、、、、                                new ThreadDemo().start();new ThreadDemo().start();}}}}

注意:该例中第一次出现的变量i是实例变量,而每次创建线程对象时候,Thread-0和Thread-1两个线程对象不能共享实例变量i。即使用继承Thread方法创建线程对象时,多个线程之间无法共享线程类的实例变量。


2.实现Runnable接口

public interface Runnable

定义Runnable接口的实现类,并重写该接口的run()方法,该run()方法同样是该线程的执行体。创建该Runnable实现类的实例,并将此实例作为Thread的target(即构造函数中的参数)来创建Thread对象(该Thread对象才是真正的线程对象,只是该Thread对象负责执行其target的run()方法)。最后调用线程对象的start()方法来启动该线程。eg:

public class ThreadDemo implements Runnable {private int i;<strong>public void run</strong>(){for(; i<100 ;i++){//当线程类实现Runnable接口时,只能通过Thread.currentThread()方法获得当前线程System.out.println(Thread.currentThread().getName() +" "+ i);}}public static void main(String[] args) {for(int i = 0 ;i<100; i++){System.out.println(Thread.currentThread().getName()+" "+i);if(i==20){ThreadDemo td = new ThreadDemo();//创建两个Thread对象,并且均把Runnable接口实例对象作为targetnew Thread(td).start();new Thread(td).start();}}}}

从运行的结果可以看到实例变量i的输出是连续的。也就是使用Runnable接口创建的多个线程是可以共享线程类的实例变量,这是因为多个线程可以共享同一个target,所以多个线程可以共享同一个线程类(target类)的实例属性。

3.使用Callable和Future

public interface Future<V> 

public interface Callable<V>

public interfaceRunnableFuture<V> extendsRunnable, Future<V>

public class FutureTask<V>extends Object implementsRunnableFuture<V>

      Java5之后,提供了Callable接口,看起来像是Runnable接口的增强版:该接口提供call()方法来作为线程执行体。与run()相比,call()方法更强大,该方法可以有返回值,并且可以声明抛出异常

     但是Callable不是Runnable接口的子类,不能作为Thread的target。而且call()方法返回的值如何调用呢?

     Java5给出了Future接口,来获得call()方法的返回值。并提供了FutureTask实现类,该类实现了Runnale接口和Future接口---可以作为Thread类的target。

     创建并启动有返回值的线程的步骤如下:

     创建Callable接口的实现类,并实现call()方法,该方法有返回值;创建Callable实现类的实例,使用FutureTask来包装Callable对象,并且也封装了call()方法的返回值;使用FutureTask作为Thread类的target创建并启动线程;调用FutureTask对象的get()方法返回子线程执行结束后的返回值。

import java.util.concurrent.Callable;import java.util.concurrent.FutureTask;public class CallableDemo implements Callable<Integer>{//实现call()方法,作为线程执行体public Integer call(){int i = 5;for( ; i<100 ; i++){System.out.println(Thread.currentThread().getName() + "的循环变量i的值:" +i);}//call()方法可以有返回值return i;}public static void main(String[] args) {//创建Callable对象CallableDemo cd = new CallableDemo();//使用FutureTask来包装Callable对象FutureTask<Integer> task = new FutureTask<Integer>(cd);for(int i=0 ; i<100 ; i++){System.out.println(Thread.currentThread().getName() + "的循环变量i的值:" +i);if(i==20){//实质还是以Callable对象来创建并启动线程new Thread(task,"有返回值的线程").start();}}try{System.out.println("子线程的返回值" + task.get());}catch(Exception e){e.printStackTrace();}}}

 FutureTask是Future接口的实现类。Future接口提供了一些方法来控制他关联的Callable任务。

booleancancel(boolean mayInterruptIfRunning)

Attempts to cancel execution of this task. //视图取消该Future关联的Callable任务
Vget()
Waits if necessary for the computation to complete, and then retrieves its result.
//返回call()方法的返回值,调用该方法将导致程序阻塞,必须等到子线程结束后才会有返回值
Vget(long timeout,TimeUnit unit)
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
//设置阻塞时间
booleanisCancelled()
Returns true if this task was cancelled before it completed normally.
booleanisDone()
Returns true if this task completed.
Callable有泛型限制,Callable接口里的泛型形参类型与Call()方法返回值类型相同。


4.创建线程的三种方式对比

实现Runnable和Callable接口的方法基本相同,只不过Callable接口定义的方法可以有返回值,而且可以声明抛出异常而已。

因此采用实现Runnable和Callable接口方式创建多线程——

优势:

1.线程类只是实现了接口类,还可以继承其他类

2.在这种方式下,多个线程可以共享同一个target对象,所以非常适合多个相同线程类处理同一份资源的情况。

劣势:

编程稍稍复杂,如果要访问当前线程必须使用Thread.currentThread()方法

而继承Thread方式则与之相反,因为已经继承了Thread类,不能再继承其他类。编写简单,如果要访问当前线程,直接使用this即可获得当前线程。

故一般建议采用实现Runnable、Callable接口的方式实现多线程。

0 0
原创粉丝点击