深入感受多线程的三种创建方式&线程的生命周期

Java多线程的三种创建方式的实现以及区别
  • 直接继承Thread类
public class MyThreadOfExtendsThread extends Thread {
    @Override
    public void run() {
        super.run();
        System.out.println("继承Thread 重写覆盖父类run方法 实现一个线程" + Thread.currentThread().getName());
    }
}
  • 实现Runnable接口
public class MyThreadOfImplementsRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("实现Runnable接口 实现重写接口run方法 "+ Thread.currentThread().getName());
    }

    /*
     * 两种实现创建线程的方式 本质都是实现了Runnable 接口
     */
    public static void main(String[] args) {
        MyThreadOfImplementsRunnable myThreadOfImplementsRunnable = new MyThreadOfImplementsRunnable();

        MyThreadOfExtendsThread myThreadOfExtendsThread = new MyThreadOfExtendsThread();
        Thread t = new Thread(myThreadOfImplementsRunnable);
        t.start();
        myThreadOfExtendsThread.start();
    }
}
  • 实现Callable接口
public class MyThreadOfImplementsCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("实现callable接口 实现call方法 利用返回结果<String or Integer>搞事情"+ Thread.currentThread().getName());
        Thread.sleep(1000);
        return "利用返回值要搞事情";
    }

    /**
     * 那么这三种方式的区别是啥呢? 什么情况下该用哪一种呢?
     * 首先 最好区分的就是实现callable接口的方式 执行线程能够得到一个返回值 可以利用这个返回值去搞事情
     * 其次 对于直接继承Thread类的方式 和 实现 Runnable 接口
     * * Java是单继承的方式 一个类只能继承一个父类 如果你的类已经继承了其他类 此时想创建线程 那么久实现 Runnable接口比较方便
     * * 本质上都是实现了Runnable接口 Thread类里面也是实现了Runnable接口 所以使用 继承Runnable接口方式比较灵活
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 这里跟其他两种方式不一样 需要用到线程池来执行
        ExecutorService myES = Executors.newFixedThreadPool(1);
        Future<String> future = myES.submit(new MyThreadOfImplementsCallable());
        // future.get()是一个阻塞方法 等call()方法return返回之后 才会得到值
        System.out.println(new StringBuilder().append(Thread.currentThread().getName()).append("-->").append(future.get()).toString());
    }
}

思考:三种创建方式的区别 以及如何去选择?
以下是callable接口和 Runnable接口区别

  • The {@code Callable} interface is similar to {@link

  • java.lang.Runnable}, in that both are designed for classes whose
  • instances are potentially executed by another thread. A
  • {@code Runnable}, however, does not return a result and cannot
  • throw a checked exception.
  • The {@link Executors} class contains utility methods to

  • convert from other common forms to {@code Callable} classes.

线程的生命周期

Java线程从创建到销毁一共可能经历6个过程:

  1. new: 初始状态,线程被构建,但是还没有调用start方法:然后交给OS去对应有个调度算法(抢占式,时间片)去运行
  2. Runnabled:运行状态,JAVA线程把操作系统当真的就绪和运行两种状态统一称为“运行中”
  3. blocked:阻塞状态,表示线程进入了等待状态,也就是线程因为某种原因放弃CUP使用权,阻塞也分为三种情况
  1. waiting:等待状态
  2. time_waiting:超市等待状态,超时以后字段返回
  3. terminated:终止状态,表示当前线程执行完毕

线程的生命周期

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值