创建多线程有几种方法

如题所述

1、通过继承Thread类创建线程

(1).首先定义一个类去继承Thread父类,重写父类中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。
(2).直接创建一个ThreadTest类的对象,也可以利用多态性,变量声明为父类的类型。

(3).调用start方法,线程启动,隐含的调用run()方法。

[java] view plain copy

    public class ThreadTest extends Thread{  

    public void run(){  

    for(int i=0;i<=10;i++){  

    System.out.println(i);  

    }         

    }  

    public static void main(String[] args) {  

    ThreadTest thread1=new ThreadTest();  

    ThreadTest thread2=new ThreadTest();  

    thread1.start();  

    thread2.start();  

    }  

    }  

    2、通过实现Runnable接口创建线程

    (1).定义一个类实现Runnable接口,重写接口中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。

    (2).创建Runnable接口实现类的对象。

    (3).创建一个ThreadTest类的对象,需要封装前面Runnable接口实现类的对象。(接口可以实现多继承)

    (4).调用Thread对象的start()方法,启动线程

    [java] view plain copy

    public class ThreadTest implements Runnable{  

    @Override  

    public void run() {  

    for(int i=0;i<=10;i++){  

    System.out.println(i);  

    }     

    }  

    public static void main(String[] args) {  

    ThreadTest threadTest=new ThreadTest();  

    Thread theard=new Thread(threadTest);  

    theard.start();  

    }  

    }  


    3.通过Callable和Future创建线程

    (1)创建Callable接口的实现类,并实现call()方法,该call()方法将作为线程执行体,并且有返回值。

    (2)创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callable对象的call()方法的返回值。

    (3)使用FutureTask对象作为Thread对象的target创建并启动新线程。

    (4)调用FutureTask对象的get()方法来获得子线程执行结束后的返回值

    [java] view plain copy

    public class ThreadTest implements Callable<Integer>{  

    @Override  

    public Integer call() throws Exception {  

    int count =0;  

    for(int i=0;i<=10;i++){  

    count=count+i;  

    }  

    return count;     

    }  

    public static void main(String[] args) throws InterruptedException, ExecutionException {  

    ThreadTest test=new ThreadTest();  

    FutureTask<Integer> thread = new FutureTask<>(test);  

    new Thread(thread,"有返回值的线程").start();    

    System.out.println(thread.get());  

    }  

    }  

    使用实现Runnable接口方式创建线程可以共享同一个目标对象(TreadDemo1 tt=new TreadDemo1();),实现了多个相同线程处理同一份资源。

    然后再看一段来自JDK的解释:

    The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments calledrun.

    This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable is implemented by classThread. Being active simply means that a thread has been started and has not yet been stopped.

    In addition, Runnable provides the means for a class to be active while not subclassingThread. A class that implementsRunnable can run without subclassingThread by instantiating aThread instance and passing itself in as the target. In most cases, theRunnable interface should be used if you are only planning to override therun() method and no otherThread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

    采用实现Runnable、Callable接口的方式创见多线程时,优势是:

    线程类只是实现了Runnable接口或Callable接口,还可以继承其他类。

    在这种方式下,多个线程可以共享同一个target对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU、代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。

    劣势是:

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

    采用继承Thread类方式:
    (1)优点:编写简单,如果需要访问当前线程,无需使用Thread.currentThread()方法,直接使用this,即可获得当前线程。
    (2)缺点:因为线程类已经继承了Thread类,所以不能再继承其他的父类。
    采用实现Runnable接口方式:
    (1)优点:线程类只是实现了Runable接口,还可以继承其他的类。在这种方式下,可以多个线程共享同一个目标对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。
    (2)缺点:编程稍微复杂,如果需要访问当前线程,必须使用Thread.currentThread()方法。

温馨提示:答案为网友推荐,仅供参考
相似回答