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();
            }
        }
    
  • 相关阅读:
    使用jedis连接redis
    布隆过滤器redis缓存
    SQL与NOSQL
    Charles 移动端抓包工具,使用方法以及注意事项
    安装npm包的时候报错rollbackFailedOptional: verb npm-session
    You may need an appropriate loader to handle this file type.
    数组去重
    判断两个数组是否相等(包括数组里边的键值对是否相等)
    数组里的字符串转为数字
    背景色铺满整个屏幕
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/14603496.html
Copyright © 2011-2022 走看看