创建线程的4种方法

使用new Thread()创建一个空线程

@Slf4j
public class EmptyThreadDemo {
    public static void main(String[] args){
        Thread thread = new Thread();
        log.info("线程名称:{}",thread.getName());
        log.info("线程ID:{}",thread.getId());
        log.info("线程状态:{}",thread.getState());
        log.info("线程优先级:{}",thread.getPriority());
        thread.start();
    }
}

控制台输出:
请添加图片描述

1、extends Thread

@Slf4j
public class DemoThread extends Thread{
    public static final int MAX_TURN = 5;
    static int threadNo = 1;

    public DemoThread(){
        super("DemoThread:" + threadNo++);
    }

    @Override
    public void run() {
        for (int i=1;i<MAX_TURN;i++){
            log.info(getName() + ",轮次:"+i);
        }
        log.info(getName() + "运行结束。");
    }
}

@Slf4j
public class CreateDemo{
    public static void main(String[] args){
        Thread thread;
        for (int i =0;i<2;i++){
            thread = new DemoThread();
            thread.start();
        }
        log.warn(Thread.currentThread().getName() + "运行结束。");
    }
}

控制台输出:
请添加图片描述

2、implements Runnable

public class DemoThread2 implements Runnable{
    public static final int MAX_TURN = 5;

    @Override
    public void run() {
        for (int i=1;i<MAX_TURN;i++){
            log.info(getName() + ",轮次:"+i);
        }
        log.info(getName() + "运行结束。");
    }

    private String getName(){
        return Thread.currentThread().getName();
    }
}
public class CreateDemo2 {
    static int threadNo = 1;
    public static void main(String[] args){
        Thread thread;
        for (int i= 0;i<2;i++){
            thread = new Thread(new DemoThread2(),"runnable thread" + threadNo++);
            thread.start();
        }
    }
}

控制台输出:
请添加图片描述

优雅创建Runnable线程的方式:匿名内部类/Lambda表达式
@Slf4j
public class CreateRunnableThreadDemo {
    public static final int MAX_TURN = 5;
    static int threadNo = 1;
    public static void main(String[] args){
      //匿名内部类
        for (int i=0;i<2;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=1;i<MAX_TURN;i++){
                        log.info(getName() + ",轮次:"+i);
                    }
                    log.info(getName() + "运行结束。");
                }
            },"RunnableThread" + threadNo++).start();
        }

    }
    private static String getName(){
        return Thread.currentThread().getName();
    }
}
@Slf4j
public class CreateRunnableThreadDemo {
    public static final int MAX_TURN = 5;
    static int threadNo = 1;
    public static void main(String[] args){
      //Lambda表达式
        for (int i=0;i<2;i++){
            new Thread(() -> {
                for (int i1 = 1; i1 <MAX_TURN; i1++){
                    log.info(getName() + ",轮次:"+ i1);
                }
                log.info(getName() + "运行结束。");
            },"RunnableThread" + threadNo++).start();
        }

    }
    private static String getName(){
        return Thread.currentThread().getName();
    }
}

3、使用Callable和FutureTask创建线程

@Slf4j
public class ReturnableTask implements Callable<Long> {
    public static final int COMPUTE_TIMES = 100_000_000;

    @Override
    public Long call() throws Exception {
        long startTime = System.currentTimeMillis();
        log.info(getName() + "线程运行开始。");
        Thread.sleep(1000);
        for (int i = 0;i<COMPUTE_TIMES;i++){
            int j = i * 10000;
        }
        long used = System.currentTimeMillis() - startTime;
        return used;
    }

    private String getName(){
        return Thread.currentThread().getName();
    }
}
@Slf4j
public class CreateDemo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ReturnableTask returnableTask = new ReturnableTask();
        FutureTask<Long> futureTask = new FutureTask<>(returnableTask);
        Thread thread = new Thread(futureTask,"returnableThread");
        thread.start();
        log.info(Thread.currentThread().getName() + "让子弹飞一会儿");
        log.info(Thread.currentThread().getName() + "做一点自己的事情");
        for (int i=0;i<ReturnableTask.COMPUTE_TIMES/2;i++){
            int j = i * 10000;
        }
        log.info(Thread.currentThread().getName() + "获取并发任务的执行结果:");
        log.info(thread.getName() + "线程占用时间(ms):" + futureTask.get());
        log.info(Thread.currentThread().getName() + " END");

    }
}

控制台输出:
请添加图片描述
请添加图片描述

线程池

线程池-创建

线程池ThreadPoolExecutor的基本配置

参考

《Java高并发核心编程(卷2)》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值