首页 > 其他分享 >轮询一段时间后退出的两种实现

轮询一段时间后退出的两种实现

时间:2022-09-05 09:36:16浏览次数:46  
标签:轮询 一段时间 System new result sid 退出 scheduledExecutorService wsLinkViewResp

一、循环 + Thread.sleep()

	long startTime = System.currentTimeMillis();
        do {
            System.out.println("run "+ System.currentTimeMillis());

            WsLinkViewResp wsLinkViewResp = viewLinkService
                    .checkInspectionStatus((WsLinkViewReq) object).getData();
            sendMessage(sid, wsLinkViewResp.toString());
            if (wsLinkViewResp.getFinishedNum().equals(wsLinkViewResp.getSumNum())) {
                closeWithReason(sid, "所有作业已执行完成");
            }
            try {
                Thread.sleep(1000);
            }
            catch (Exception e) {
                e.printStackTrace();
            }

        } while (System.currentTimeMillis() - startTime < CHECK_TIME);

二、ScheduledExecutorService每隔一段时间执行一个任务

ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
                new ThreadFactoryBuilder().setNameFormat("check-inspection-status-scheduler-%d").build());


        AtomicLong waitTime = new AtomicLong(0);
        try {

            while (waitTime.get() < CHECK_TIME) {

                ScheduledFuture<WsLinkViewResp> future = scheduledExecutorService.schedule(() -> {

                    System.out.println(object.toString());

                    WsLinkViewResp wsLinkViewResp = viewLinkService
                            .checkInspectionStatus((WsLinkViewReq) object).getData();
                    System.out.println(wsLinkViewResp.toString());
                    waitTime.addAndGet(PERIOD);
                    return wsLinkViewResp;

                }, PERIOD, TimeUnit.MILLISECONDS);

                WsLinkViewResp result = future.get();

                if (result.getFinishedNum().equals(result.getSumNum())) {
                    close(sid, "所有作业已执行完成");
                    return;
                }
                else {
                    sendResp(sid, result);
                }
            }
            close(sid, "执行时间已超过五分钟,请合理配置环节视图或稍后查看相关作业状态!");

        } catch (ExecutionException | InterruptedException e) {
            close(sid, "服务端发生中断异常");
            Thread.currentThread().interrupt();
        }
        finally {
            scheduledExecutorService.shutdown();
        }

尝试历史

        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
                new ThreadFactoryBuilder().setNameFormat("check-inspection-status-scheduler-%d").build());
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("gghjghjghgjgjghj");
                WsLinkViewResp wsLinkViewResp = viewLinkService
                        .checkInspectionStatus((WsLinkViewReq) object).getData();
                sendMessage(sid, "dsgdgdfsggsg");

                if (wsLinkViewResp.getFinishedNum().equals(wsLinkViewResp.getSumNum())) {
                    scheduledExecutorService.shutdown();
                }
            }
        }, 0, PERIOD, TimeUnit.MILLISECONDS);

        try {
            Thread.sleep(CHECK_TIME);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        scheduledExecutorService.shutdownNow();



        long startTime = System.currentTimeMillis();
        do {
            System.out.println("run "+ System.currentTimeMillis());

            WsLinkViewResp wsLinkViewResp = viewLinkService
                    .checkInspectionStatus((WsLinkViewReq) object).getData();
            sendMessage(sid, wsLinkViewResp.toString());
            if (wsLinkViewResp.getFinishedNum().equals(wsLinkViewResp.getSumNum())) {
                closeWithReason(sid, "所有作业已执行完成");
            }
            try {
                Thread.sleep(1000);
            }
            catch (Exception e) {
                e.printStackTrace();
            }

        } while (System.currentTimeMillis() - startTime < CHECK_TIME);

        IViewLinkService viewLinkService = SpringUtils.getBean(ViewLinkServiceImpl.class);

        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
                new ThreadFactoryBuilder().setNameFormat("check-inspection-status-scheduler-%d").build());


        AtomicLong waitTime = new AtomicLong(0);
        try {

            while (waitTime.get() < CHECK_TIME) {

                ScheduledFuture<WsLinkViewResp> future = scheduledExecutorService.schedule(() -> {

                    System.out.println(object.toString());

                    WsLinkViewResp wsLinkViewResp = viewLinkService
                            .checkInspectionStatus((WsLinkViewReq) object).getData();
                    System.out.println(wsLinkViewResp.toString());
                    waitTime.addAndGet(PERIOD);
                    return wsLinkViewResp;

                }, PERIOD, TimeUnit.MILLISECONDS);

                WsLinkViewResp result = future.get();

                if (result.getFinishedNum().equals(result.getSumNum())) {
                    close(sid, "所有作业已执行完成");
                    return;
                }
                else {
                    sendResp(sid, result);
                }
            }
            close(sid, "执行时间已超过五分钟,请合理配置环节视图或稍后查看相关作业状态!");

        } catch (ExecutionException | InterruptedException e) {
            close(sid, "服务端发生中断异常");
            Thread.currentThread().interrupt();
        }
        finally {
            scheduledExecutorService.shutdown();
        }

标签:轮询,一段时间,System,new,result,sid,退出,scheduledExecutorService,wsLinkViewResp
From: https://www.cnblogs.com/acelin/p/16656924.html

相关文章