创建多线程有四种方式

创建多线程有哪几种方式

创建多线程共有四种方式:

  1. 继承Thread类创建多线程
  2. 实现Runnable接口创建多线程
  3. 实现Callable接口通过FutureTask包装器来创建Thread多线程
  4. 使用ExecutorService、Callable、Future实现有返回结果的线程。

1. 继承Thread类

2. 实现Runnable接口

3. Callable + Future

4. ExecutorService + Callable + Future

代码如下:

  1. 继承Thread类
package com.dazhi.thread.multithread;

/**
 * 多线程创建,继承
 * @author dazhi
 */
public class MyThread extends Thread{

    @Override
    public void run() {
        for (int i=0; i<10; i++) {
            System.out.println(Thread.currentThread().getName() +":"+ i);
        }
    }

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

        MyThread thread1 = new MyThread();
        MyThread thread2 = new MyThread();
        MyThread thread3 = new MyThread();

        thread1.start();
        thread2.start();
        thread3.start();

    }
}
  1. 实现Runnable接口
package com.dazhi.thread.multithread;

/**
 * 多线程创建, 实现Runnable
 * @author dazhi
 */
public class MyRunnable implements Runnable {

    @Override
    public void run() {
        for (int i=0; i<10; i++) {
            System.out.println(Thread.currentThread().getName() +":"+ i);
        }
    }

    public static void main(String[] args) {

        MyRunnable myRunnable = new MyRunnable();
        Thread thread1 = new Thread(myRunnable, "thread1");
        Thread thread2 = new Thread(myRunnable, "thread2");
        Thread thread3 = new Thread(myRunnable, "thread3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}
  1. 实现Callable
package com.dazhi.thread.multithread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 多线程创建, 实现Callable接口
 * @author dazhi
 */
public class MyCallable implements Callable<Object> {

    @Override
    public Object call() throws Exception {
        return "嗨,你好";
    }

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

        MyCallable myCallable = new MyCallable();
        FutureTask<Object> futureTask = new FutureTask<>(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        
        // 可以拿到线程的返回值
        System.out.println(futureTask.get());
    }
}

// 运行结果
嗨,你好

Process finished with exit code 0
  1. ExecutorService + Callable + Future
package com.dazhi.thread.multithread;

import java.util.concurrent.*;

/**
 * 多线程创建, ExecutorService + Callable + Future
 * @author dazhi
 */
public class MyExecutors {

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

        // 不提倡的方式创建线程池方式。为了方便就这样写了
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        Future<Integer> submit = executorService.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 1234;
            }
        });
        executorService.shutdown();
        System.out.println(submit.get());
    }
}
// 运行结果
1234

Process finished with exit code 0

// 不提倡创建线程池的原因
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值