栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

并发编程之FutureTask 异步编程 三个方法并行 十个接口合成一个

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

并发编程之FutureTask 异步编程 三个方法并行 十个接口合成一个

十个接口合成一个接口

一. main程序示例
public class UseFutureTask {

    
    private static class UseCallable implements Callable {
        private int sum;

        @Override
        public Integer call() throws Exception {
            Thread.sleep(2000);
            for (int i = 0; i < 5000; i++) {
                //如果要中断任务,  1.主类调用futureTask.cancel(true);   2.加上下面这段话要加上去
//	        	if(Thread.currentThread().isInterrupted()) {
//					System.out.println("Callable子线程计算任务中断!");
//					return null;
//				}
                sum = sum + i;
            }
            return sum;
        }
    }

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

        UseCallable useCallable = new UseCallable();
        FutureTask futureTask = new FutureTask<>(useCallable);
        new Thread(futureTask).start();
        Integer integer = futureTask.get();
        System.out.println("futuretask线程拿到的结果:" + integer);
        futureTask.cancel(true);
    }

}
二. 项目使用示例(首页获取用户、部门、优惠券)

1.service层(方法实现Callable接口)

@Service
public class UserServiceImpl implements Callable {

    public UserEntity getUserEntity() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setName("张三");
        userEntity.setAddress("闽侯");
        userEntity.setPhone("18060734774");
        return userEntity;
    }


    @Override
    public UserEntity call() {
        return getUserEntity();
    }
}
@Service
public class DeptServiceImpl implements Callable {

    public DeptEntity getDeptEntity() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        DeptEntity deptEntity = new DeptEntity();
        deptEntity.setName("张三");
        deptEntity.setDesc("部门地址");
        return deptEntity;
    }

    @Override
    public DeptEntity call() throws Exception {
        return getDeptEntity();
    }
}
@Service
public class CouponServiceImpl implements Callable {

    public Coupon getCoupon() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Coupon coupon = new Coupon();
        coupon.setUsed(5);
        coupon.setUsedAble(6);
        coupon.setOverTime(7);
        return coupon;
    }

    @Override
    public Coupon call() throws Exception {
        return getCoupon();
    }
}

2.controller层(首页获取用户、部门、优惠券信息)

 @GetMapping("/homePage2")
    public HomePageVO homePage2() throws ExecutionException, InterruptedException {
        HomePageVO homePageVO = new HomePageVO();

		//运行三个线程
        FutureTask userEntityFutureTask = new FutureTask<>(userService);
        new Thread(userEntityFutureTask).start();
        FutureTask deptEntityFutureTask = new FutureTask<>(deptService);
        new Thread(deptEntityFutureTask).start();
        FutureTask couponFutureTask = new FutureTask<>(couponService);
        new Thread(couponFutureTask).start();

		//获取三个线程的返回值
        UserEntity userEntity = userEntityFutureTask.get();
        userEntityFutureTask.cancel(true);
        DeptEntity deptEntity = deptEntityFutureTask.get();
        deptEntityFutureTask.cancel(true);
        Coupon coupon = couponFutureTask.get();
        couponFutureTask.cancel(true);

        homePageVO.setUserEntity(userEntity);
        homePageVO.setDeptEntity(deptEntity);
        homePageVO.setCoupon(coupon);
  
        return homePageVO;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351165.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号