自动排考场更新进度实现代码分析

本文介绍了一种基于前后端分离架构的线程同步机制,前端通过定时器请求查询线程状态,后台则通过线程服务更新线程表中的数据。具体实现包括线程状态的查询、线程启动与停止、线程状态更新等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端设置定时器请求查询线程表中的数据;
后台设置线程更新线程表中的数据;

    /**
     * @return
     */
    @NeedAdmin
    @Get("autoarrangeexamroom")
    public String autoarrangeexamroom() {
        ImplService service = apkcService;
        // 调取批量代选课线程
        threadService.startThread(inv, dw,key,ThreadType.ZDPKC.getCHValue(), ThreadType.ZDPKC.getValue(), yh.getCxid(), yh.getYhm(), yh.getYhxm(), schoolid, service,
                jsonObject);
    }


    public String startThread(final Invocation inv, final Dw dw, final String xckey, final String name, final int type, long cxid, String username,
        String realname, final int schoolid, final ImplService service, final JSONObject jsonObject) {

        // 线程启动状态
        boolean status = false;

        // 通过key和模块类型及单位信息获取线程状态
        XcLog xcLog = xcLogDAO.find(xckey, type, schoolid);

        // 线程状态判断
        if (xcLog != null) {
            int xcstatus = xcLog.getStatus();
            if (xcstatus != 0) {
                status = true;
            }
            if (xcstatus == 1) {
                xcLogDAO.updateStatus(2, xcLog.getId());
            }
        }
        else {
            status = true;
        }

        if (status) {
            // 新增线程
            final long threadId = this.setThread(inv, xckey, name, type, cxid, username, realname, schoolid, service, jsonObject);

            try {
                ThreadService task = new ThreadService(inv, dw, service, jsonObject, threadId, xcLogDAO);
                Thread th = new Thread(task);
                th.start(); // 
                Thread.sleep(1000);
                Thread.yield();// 
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "线程已启动";
    }



public class ThreadService implements Runnable {
    private ImplService service;
    public void run(){
        String msg = service.thread(inv, dw, jsonObject, threadId); //线程任务
    }
}


public class APKCService implements ImplService {
    public String thread(Invocation inv, Dw dw, JSONObject jsonObject, long threadId) {
        commonService.threadService.updateNum(total, -1, successamount, erroramount, threadId);
    }
}

public int updateNum(long total, long nowamount, long successamount, long erroramount, long id) {
    // TODO: handle exception
    int result = 0;
    try {
        result = xcLogDAO.updateNum(xcLog); //更新线程表中数据
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

html

<td align="center" id="numMsg"></td>

前端js

//按钮点击触发定时器
setInterval(getXLog(),3000);
//
function getXLog(){
            $.ajax({
            type : "get",
            url : "/api/getThreadInfoByKey",
            data : {
                "key" : key, 
                "type" : type,
                "schoolid" : schoolid
            },
            success : function(data){
                var json = JSON.parse(data);   //json.xcLog.total
                if(json.xcLog.nowamount == ksykTotal || json.xcLog.status !=0){
                    $("#numMsg").html('本次安排考场结束');
                    clearInterval(myvar[checkid]); //结束定时器
                }else{
                    $("#numMsg").html(json.xcLog.nowamount+'/'+ksykTotal);
                }
            }
        });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值