Java Lambda表达式 常用工具类

Runnable

public static void main(String[] args)
{
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.submit(() ->
    {
        Thread.currentThread().setName("I am child thread");
        try
        {
            Thread.sleep(2);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + ", id: " + Thread.currentThread().getId());
    });
}

Callable

public static void main(String[] args)
{
    ExecutorService executorService = Executors.newCachedThreadPool();
    Future<Double> future = executorService.submit(() ->
    {
        Thread.currentThread().setName("I am child thread.");
        System.out.println(Thread.currentThread().getName() + ", id: " + Thread.currentThread().getId());
        Random random = new Random();
        Double weight = random.nextDouble();
        System.out.println("weight: " + weight);
        return weight;
    });
    executorService.shutdown();

    try
    {
        Double result = future.get(1, TimeUnit.SECONDS);
        System.out.println("Result: " + result);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    catch (ExecutionException e)
    {
        e.printStackTrace();
    }
    catch (TimeoutException e)
    {
        e.printStackTrace();
    }
}

FutrueTask

public static void main(String[] args)
{
    ExecutorService executorService = Executors.newCachedThreadPool();

    FutureTask<Double> futureTask = new FutureTask<Double>(() ->
    {
        Thread.currentThread().setName("I am child thread.");
        System.out.println(Thread.currentThread().getName() + ", id: " + Thread.currentThread().getId());
        Random random = new Random();
        Double weight = random.nextDouble();
        System.out.println("weight: " + weight);
        return weight;
    });

    executorService.submit(futureTask);
    executorService.shutdown();

    try
    {
        Double result = futureTask.get(2, TimeUnit.SECONDS);
        System.out.println("Result: " + result);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    catch (ExecutionException e)
    {
        e.printStackTrace();
    }
    catch (TimeoutException e)
    {
        e.printStackTrace();
    }
}

CompletableFutrue

public class CompletableFutrueTest
{
    public static void main(String[] args)
    {
        try
        {
//			CompletableFutrueTest.runAync();
//			CompletableFutrueTest.supplyAsync();
//          CompletableFutrueTest.whenComplete();
//            CompletableFutrueTest.thenApply();
//            CompletableFutrueTest.handle();
            CompletableFutrueTest.thenAccept();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    //无返回值
    private static void runAync() throws Exception
    {
        CompletableFuture future = CompletableFuture.runAsync(() ->
        {
            try
            {
                TimeUnit.SECONDS.sleep(1);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            System.out.println("run end...");
        });
        future.get();
    }

    //有返回值
    private static void supplyAsync() throws Exception
    {
        CompletableFuture<Long> future = CompletableFuture.supplyAsync(() ->
        {
            try
            {
                TimeUnit.SECONDS.sleep(1);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            System.out.println("run end ...");
            return System.currentTimeMillis();
        });

        long time = future.get();
        System.out.println("time: " + time);
    }

    //计算结果完成时的回调方法
    private static void whenComplete() throws Exception
    {
        //链式编程简洁
        //创建一个异步操作
        CompletableFuture.runAsync(() ->
        {
            try
            {
                TimeUnit.SECONDS.sleep(1);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            if (new Random().nextInt(10) % 2 == 0)
            {
                int i = 12 / 0;
            }
            System.out.println("run end ...");

        }).exceptionally((throwable) ->
        {
            System.out.println("执行失败! " + throwable.getMessage());
            return null;
        }).whenComplete((aVoid, throwable) ->
        {
            System.out.println("执行完毕");
        });

        TimeUnit.SECONDS.sleep(2);
    }

    //当一个线程依赖另一个线程
    private static void thenApply() throws Exception
    {
        CompletableFuture future = CompletableFuture.supplyAsync(() ->
        {
            long result = new Random().nextInt(100);
            System.out.println("result1=" + result);
            return result;
        }).thenApply(t ->
        {
            long result = t * 5;
            System.out.println("result2=" + result);
            return result;
        });

        long result = (long) future.get();
        System.out.println(result);
    }

    //执行任务完成时对结果的处理
    //handle 方法和 thenApply 方法处理方式基本一样。
    //不同的是 handle 是在任务完成后再执行,还可以处理异常的任务。
    //thenApply 只可以执行正常的任务,任务出现异常则不执行 thenApply 方法。
    private static void handle() throws Exception
    {
        CompletableFuture future = CompletableFuture.supplyAsync(() ->
        {
            int i = 10 / 0;
            return new Random().nextInt(100);
        }).handle((param, throwable) ->
        {
            int result = -1;
            if (null == throwable)
            {
                result = param * 5;
            }
            else
            {
                System.out.println("error: " + throwable.getMessage());
            }
            return result;
        });
        System.out.println(future.get());
    }

    //接收任务的处理结果,并消费处理,无返回结果。
    private static void thenAccept() throws Exception
    {
        CompletableFuture future = CompletableFuture.supplyAsync(() ->
        {
            return new Random().nextInt(100);
        }).thenAccept((param ->
        {
            System.out.println("result: " + param);
        }));

        future.get();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值