Java实现异步开发的方式

 1)、继承 Thread
  2)、实现 Runnable 接口
 3)、实现 Callable 接口 + FutureTask (可以拿到返回结果,可以处理异常)
 4)、使用线程池

 区别:1、2)不能得到返回值
           3)可以获得返回值
           但1、2、3都不能控制资源,会造成系统资源浪费
           只有4)可以控制资源,优点是性能稳定

所以在在业务代码开发中,1、2、3)启动线程的方式都不用,应该将所有的多线程异步任务交给线程池来执行。

示例代码:

public class ThreadTest {

    public static ExecutorService executorService= Executors.newFixedThreadPool(10);

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

        System.out.println("main start.........");
        //一.extends Thread
//        Thread01 thread01=new Thread01();
//        new Thread(thread01).start();
        //二.implements Runnable
//        Runnable01 runnable01=new Runnable01();
//        new Thread(runnable01).start();
        //三.implements Callable
//        FutureTask<Integer> futureTask=new FutureTask<>(new Callable01());
//        new Thread(futureTask).start();
//        //futureTask.get方法会阻塞直到拿到结果
//        Integer result =  futureTask.get();
//        System.out.println("main end........."+result);
        //四.使用线程池的方式实现异步编程
        executorService.execute(new Runnable01());
    }

    public static class  Thread01 extends Thread{
         public void run(){
             System.out.println("当前线程:"+Thread.currentThread().getId());
             Integer i=10/2;
             System.out.println("运算结果.........:"+i);
         }
    }

    public static class Runnable01 implements Runnable{
        @Override
        public void run() {
            System.out.println("当前线程:"+Thread.currentThread().getId());
            Integer i=10/2;
            System.out.println("运算结果.........:"+i);
        }
    }

    public static class Callable01 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程:"+Thread.currentThread().getId());
            Integer i=10/2;
            System.out.println("运算结果.........:"+i);
            return i;
        }
    }
}

线程池执行有2个方法,分别是execute()和submit(),它们的区别是submit方法执行会有返回值,而,execute()方法无返回值,exeucte()只能接收实现Runnable的类,而submit可接收实现Runnable或Callable的类

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值