码迷,mamicode.com
首页 > 编程语言 > 详细

线程实现连续启动停,并在某一时间段内运行

时间:2020-04-06 20:38:16      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:work   oid   json   bsp   alarm   print   min   http   imp   

 

 

Thread类实现

import org.springframework.stereotype.Component;

@Component
public class AlarmMockThread extends Thread {

    private volatile long startTime = System.currentTimeMillis();
    private volatile long endTime = System.currentTimeMillis();

    public void setTime(long startTime, long endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public void setTime(long endTime) {
        this.endTime = endTime;
    }

    @Override
    public void run() {
        try {
            while (!interrupted()) {
                long sysTime = System.currentTimeMillis();
                if (sysTime < endTime && sysTime > startTime) {
                    System.out.println("系统当前时间为:" + sysTime);
                    Thread.sleep(1000);
                }
            }
        } catch (Exception e) {
            System.out.println("服务运行出现异常:" + e);
        }
    }
}

 

控制层实现

import com.vivo.agent.alarm.common.bean.base.AlarmMockBean;
import com.vivo.agent.alarm.service.base.AlarmMockThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;

@Slf4j
@RestController
@RequestMapping("/alarm")
public class AlarmMockController {

    private AlarmMockThread thread = new AlarmMockThread();

    @RequestMapping(
            method = RequestMethod.POST,
            value = "/startAlarmMock",
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8",
            headers = "Accept=application/json")
    public void startAlarmMock(@RequestBody AlarmMockBean alarmMockBean) {
        log.info("请求到了Controller层");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            long startTime = format.parse(alarmMockBean.getStartTime()).getTime();
            long endTime = format.parse(alarmMockBean.getEndTime()).getTime();
            thread.setTime(startTime, endTime);
            thread.start();
        } catch (Exception e) {
            if (e instanceof IllegalThreadStateException) {
                thread.run();
            }
        }
    }

    @GetMapping("/stopAlarm")
    @ResponseStatus(HttpStatus.OK)
    public void stopAlarm() {
        log.info("停止报警推送接口");
        thread.interrupt();
        thread.setTime(System.currentTimeMillis() - 1);
        log.info("需要停止的线程id为:{}", thread.getId());
    }
}

 

请求的消息体格式

{
    "startTime":"2020-04-06 18:58:00",
    "endTime":"2020-04-06 19:58:00",
    "dominName":"1",
    "ServiceName":"1",
    "stuteCode":{
        "200":"80",
        "404":"20"
    }
}

 

线程实现连续启动停,并在某一时间段内运行

标签:work   oid   json   bsp   alarm   print   min   http   imp   

原文地址:https://www.cnblogs.com/cnndevelop/p/12643781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!