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();
            }
        }
    
  • 相关阅读:
    Java初学者的学习路线推荐
    C#基础(七)虚函数
    C#基础(六)--枚举的一些常用操作
    C#基础(五)--枚举的含义及其应用
    C#基础(四)--值类型和引用类型,栈和堆的含义
    C#基础(三)--运算符及条件控制语句
    Zipkin+Sleuth调用链监控集成和使用
    jenkins集成cucumber-resport报告
    微信小程序自动化,记录趟过的坑!
    解压版mysql+免破解版Navicat,好用!
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/14603496.html
Copyright © 2011-2022 走看看