zoukankan      html  css  js  c++  java
  • java异步转同步

    1.异步转同步,线程池提交任务,阻塞获取结果

    ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) ThreadUtils.newDaemonFixedThreadExecutor("PodActionService"
                , Runtime.getRuntime().availableProcessors() + 1);
    
     public void delete(String instanceId) throws ExecutionException, InterruptedException {
            Future<ResourceStatus> future = threadPoolExecutor.submit(new DeletePodThread(instanceId));
            ResourceStatus resourceStatus = future.get();
            LOG.info("instanceId : {} destroy status : {}",instanceId,resourceStatus);
    }
    

    2.线程任务,返回值类型ResourceStatus枚举

    public class DeletePodThread implements Callable<ResourceStatus> {
    
            public DeletePodThread(String instanceId) {
                this.instanceId = instanceId;
            }
    
            private String instanceId;
    
            @Override
            public ResourceStatus call() throws Exception {
                QueryWrapper<FamaVm> vmQueryWrapper = Wrappers.query();
                vmQueryWrapper.eq(FamaVm.INSTANCE_ID_COLUMN,instanceId);
                FamaVm famaVm = famaVmMapper.selectOne(vmQueryWrapper);
                if (null == famaVm){
                    return null;
                }
                QueryWrapper<FamaPin> pinQueryWrapper = Wrappers.query();
                pinQueryWrapper.eq(FamaPin.PIN_COLUMN,famaVm.getPin());
                pinQueryWrapper.eq(FamaPin.PIN_ALIAS_COLUMN,famaVm.getPinAlias());
                FamaPin famaPin = famaPinMapper.selectOne(pinQueryWrapper);
                if (null == famaPin){
                    return null;
                }
                PodService podService = new PodService(famaPin);
                DeletePodResponse deletePodResponse = podService.delete(famaVm);
                if (null != deletePodResponse.getError() && deletePodResponse.getError().getStatus().equalsIgnoreCase("NOT_FOUND" )){
                    return ResourceStatus.DELETED;
                }
                InstanceStatus resourceStatus = describeResStatus(podService,famaVm);
                while (!resourceStatus.typeIsFinished()){
                    ThreadUtils.sleep(Constants.SLEEP_TIME_MILLIS);
                    resourceStatus = describeResStatus(podService,famaVm);
                }
                famaVmMapper.updateById(famaVm);
                ResourceInstance resourceInstance = processInstanceMapper.selectById(famaVm.getResourceInstanceId());
                resourceInstance.setState(resourceStatus);
                processInstanceMapper.updateById(resourceInstance);
                return famaVm.getState();
            }
        }
    
  • 相关阅读:
    解决电脑启动桌面图标变乱
    多表查询
    windows安装rabbitmq踩坑实录
    springboot单元测试
    联想锁屏壁纸所在路径
    ArrayList数组扩容方式(基于jdk1.8)
    SpringCloud + Consul服务注册中心 + gateway网关
    springboot 文件上传及java使用post请求模拟文件上传
    kotlin + springboot启用elasticsearch搜索
    kotlin + springboot整合mybatis操作mysql数据库及单元测试
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/14603496.html
Copyright © 2011-2022 走看看