微信公众号:路人zhang
扫码关注微信公众号

回复“面试手册”,获取本站PDF版

回复“简历”,获取高质量简历模板

回复“加群”,加入程序员交流群

回复“电子书”,获取程序员类电子书

当前位置: Java > Java并发高频面试题 > 10.创建线程一共有哪几种方法?
本文链接:https://www.mianshi.online/multi-thread-create.html
  • 继承Thread类创建线程
  • 实现Runnable接口创建线程
  • 使用CallableFuture创建线程
  • 使用线程池例如用Executor框架

继承Thread类创建线程,首先继承Thread类,重写run()方法,在main()函数中调用子类实实例的start()方法。

public class ThreadDemo extends Thread {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " run()方法正在执行");
    }

}
public class TheadTest {
    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo(); 	
        threadDemo.start();
        System.out.println(Thread.currentThread().getName() + " main()方法执行结束");
    }

}

输出结果:

main main()方法执行结束
Thread-0 run()方法正在执行

实现Runnable接口创建线程:

首先创建实现Runnable接口的类RunnableDemo,重写run()方法;创建类RunnableDemo的实例对象runnableDemo,以runnableDemo作为参数创建Thread对象,调用Thread对象的start()方法。

public class RunnableDemo implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " run()方法执行中");
    }

}
public class RunnableTest {
    public static void main(String[] args) {
        RunnableDemo  runnableDemo = new RunnableDemo ();
        Thread thread = new Thread(runnableDemo);
        thread.start();
        System.out.println(Thread.currentThread().getName() + " main()方法执行完成");
}

输出结果:

main main()方法执行完成
Thread-0 run()方法执行中

使用Callable和Future创建线程:

1. 创建Callable接口的实现类CallableDemo,重写call()方法。

2. 以类CallableDemo的实例化对象作为参数创建FutureTask对象。

3. 以FutureTask对象作为参数创建Thread对象。

4. 调用Thread对象的start()方法。

class CallableDemo implements Callable<Integer> {

    @Override
    public Integer call() {
        System.out.println(Thread.currentThread().getName() + " call()方法执行中");
        return 0;
    }

}

 class CallableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new CallableDemo());
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println("返回结果 " + futureTask.get());
        System.out.println(Thread.currentThread().getName() + " main()方法执行完成");
    }

}

输出结果:

Thread-0 call()方法执行中
返回结果 0
main main()方法执行完成

使用线程池例如用Executor框架:

Executors可提供四种线程池,分别为:

  • newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
  • newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
  • newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
  • newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序执行。

下面以创建一个定长线程池为例进行说明,

class ThreadDemo extends Thread {

    @Override

    public void run() {

        System.out.println(Thread.currentThread().getName() + "正在执行");

    }

}

class TestFixedThreadPool {

        public static void main(String[] args) {

        //创建一个可重用固定线程数的线程池

        ExecutorService pool = Executors.newFixedThreadPool(2);

        //创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口

        Thread t1 = new ThreadDemo();

        Thread t2 = new ThreadDemo();

        Thread t3 = new ThreadDemo();

        Thread t4 = new ThreadDemo();

        Thread t5 = new ThreadDemo();

        //将线程放入池中进行执行

        pool.execute(t1);

        pool.execute(t2);

        pool.execute(t3);

        pool.execute(t4);

        pool.execute(t5);

        //关闭线程池

        pool.shutdown();

        }

        }

输出结果:

pool-1-thread-2正在执行
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-2正在执行
pool-1-thread-1正在执行

本站链接:https://www.mianshi.online如需勘误或投稿,请联系微信:lurenzhang888


点击面试手册,获取本站面试手册PDF完整版