JAVA中创建线程的三种方法及比较

JAVA中创建线程的方式有三种,各有优缺点,具体如下:

目录

一、继承Thread类来创建线程

二、实现Runnable接口来创建线程

三、通过Callable和Future来创建线程

四、三种方式创建线程的比较

1、继承Thread类方式:

2、实现Runnable接口的方式:

3、通过Callable和Future的方式:

  总结:


一、继承Thread类来创建线程

1、创建一个任务类,继承Thread线程类,因为Thread类已经实现了Runnable接口,然后重写run()方法,run()方法中的内容就是需要线程完成的任务。

2、创建一个任务类的对象,即创建了线程对象。

3、调用任务类对象的start()方法,来启动一个线程。

代码实例:

public class TestThread extends Thread {
    public void run() {
        for (int i = 0; i < 20; i++) {
            // 与Thread.currentThread().getName()相同
            System.out.println(this.getName() + " " + i);
        }
    }

    public static void main(String[] args) {
        TestThread t1 = new TestThread();
        TestThread t2 = new TestThread();
        t1.start();
        t2.start();
    }
}

二、实现Runnable接口来创建线程

1、创建一个任务类,实现Runnable接口,并实现run()方法,run()方法中的内容就是需要线程完成的任务。

2、创建一个任务类的对象。

3、任务类必须在线程中执行,因此将任务类的对象作为参数,创建一个Thread类对象,该Thread类对象才是真正的线程对象。

4、调用Thread线程类对象的start()方法,来启动一个线程。

代码实例:

public class TestThread implements Runnable {
    public void run() {
        for (int i = 0; i < 20; i++) {
            // 获取线程名称,默认格式:Thread-0
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }

    public static void main(String[] args) {
        TestThread tt1 = new TestThread();
        TestThread tt2 = new TestThread();
        // 可为线程添加名称:Thread t1 = new Thread(tt1, "线程1");
        Thread t1 = new Thread(tt1);
        Thread t2 = new Thread(tt2);
        t1.start();
        t2.start();
    }
}

三、通过Callable和Future来创建线程

1、创建一个任务类,实现Callable接口,并实现call()方法,call()方法中的内容就是需要线程完成的任务,且有返回值。

2、创建一个任务类的对象,并使用FutureTask类来包装任务类的对象,该FutureTask对象封装了任务类对象中call()方法的返回值。

3、任务类必须在线程中执行,因此将FutureTask类的对象作为参数,创建一个Thread类对象,该Thread类对象才是真正的线程对象。

4、调用Thread线程类对象的start()方法,来启动一个线程。

5、调用FutureTask类对象的get()方法来获取线程执行的返回值,即任务类对象中call()方法的返回值。

代码实例:

public class TestThread implements Callable<Integer> {
    public Integer call() {
        int i = 0;
        for (i = 0; i < 20; i++) {
            if (i == 5)
                break;
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        return i;
    }

    public static void main(String[] args) {
        TestThread tt = new TestThread();
        FutureTask<Integer> ft = new FutureTask<Integer>(tt);
        Thread t = new Thread(ft);
        t.start();
        try {
            System.out.println(Thread.currentThread().getName() + " " + ft.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

四、三种方式创建线程的比较

1、继承Thread类方式:

(1)优点:编写简单,任务类中访问当前线程时,可以直接使用this关键字。

(2)缺点:任务类即线程类已经继承了Thread类,所以不能再继承其他父类。

2、实现Runnable接口的方式:

(1)优点:任务类只实现了Runnable接口,还可以继承其他类。这种方式,可以多个线程对象共享一个任务类对象,即多线程共享一份资源的情况,如下:

TestThread tt1 = new TestThread();
     Thread t1 = new Thread(tt1);
     Thread t2 = new Thread(tt1);
     t1.start();
     t2.start();

(2)缺点:编写稍微复杂,任务类中访问当前线程时,必须使用Thread.currentThread()方法。

3、通过Callable和Future的方式:

(1)优点:任务类只实现了Callable接口,还可以继承其他类,同样多线程下可共享同一份资源,这种方式还有返回值,并且可以抛出返回值的异常。

(2)缺点:编写稍微复杂,任务类中访问当前线程时,必须使用Thread.currentThread()方法。

  总结:

        在仅仅只重写run()方法,而不重写Thread类其他方法的前提下,比较推荐实现Runnable接口的方式创建线程。因为不打算修改或增强类的基本能力,不应该为其创建子类。而且实现Runnable接口的方式,线程和资源相对分离,程序更加健壮,更符合面向对象的编程思想。当然,需要线程有返回值时可以使用Callable的方式,但Callable的方式有一个问题,当调用get()方法时,如果线程还未执行完毕,则会阻塞到线程执行完毕拿到返回值。

猜你喜欢

转载自blog.csdn.net/Goals1989/article/details/128718311