java多线程定时任务实例

实现功能: 执行3个线程到线程池,默认每个线程任务2秒执行一次.程序运行10秒后,修改所有线程中任务执行间隔为1秒,运行20秒后给所有线程中任务下停止命令,并停止所有线程池任务. 实体类:
package top.thread.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 用于读写线程中控制参数实体类
 * 作者:肖鹏 
 * 时间:2016年12月20日15:13:02
 */
public enum Registry {

    // instance
    INSTANCE;

    private static final Logger logger = LoggerFactory.getLogger(Registry.class);
    public Map<String, Object> value = new ConcurrentHashMap<>();

    // pool
    private final ExecutorService es = Executors.newCachedThreadPool();//Executors.newFixedThreadPool(2);


    // save 
    public void saveKey(String key, Object value) {
        this.value.put(key, value);
    }


    //get
    public Map<String, Object> getValue() {
        return this.value;
    }


    // add Thread In ThreadPool
    public void addThreadInPool(Thread thread) {
        this.es.execute(thread);

    }
    //stop
    public void shutdown() {
        this.es.shutdown();
    }
}

 
任务类:
package top.thread.thead;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.thread.entity.Registry;

import java.util.Map;

/**
 * Job01
 */
public class Job extends Thread {
    private static final Logger logger = LoggerFactory.getLogger(Job.class);
    //此线程标识名
    private String ThreadName;
    //启动或停止线程 默认执行
    private boolean isNo = true;
    //重复间隔时间(秒) 默认2秒
    private int time = 2;

    public Job(String ThreadName) {
        this.ThreadName = ThreadName;

    }

    @Override
    public void run() {
        //去内存取次线程最新指令是否继续循环
        while (isNo) {
            if (!isNo)
                break;
            logger.info(currentThread().getName() + \"正在执行。。。\");
            Object rAMData = Registry.INSTANCE.getValue().get(ThreadName + \"_time\");
            //去内存取最新间隔时间
            this.time = rAMData != null ? (int) rAMData : time;
            try {
                Thread.sleep(time * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            rAMData = Registry.INSTANCE.getValue().get(ThreadName + \"_isNo\");
            this.isNo = rAMData != null ? (boolean) rAMData : isNo;
        }
        logger.info(currentThread().getName() + \"停止\");
    }
}

main类:
package top.thread;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.thread.entity.Registry;
import top.thread.thead.Job;

/**
 * 线程测试
 */
public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        logger.info(\"线程任务开始..\");
        for (int i = 0; i < 3; i++) {
            //初始化线程
            Thread job = new Job(\"job\" + i);
            //添加线程进入线程池
            Registry.INSTANCE.addThreadInPool(job);
        }
        //休眠10秒
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info(\"修改所有任务间隔时间为1秒\");
        for (int i = 0; i < 3; i++) {
            //修改任务间隔时间为1秒
            Registry.INSTANCE.saveKey(\"job\" + i + \"_time\", 1);
        }
        //休眠10秒
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info(\"给所有线程下达命令为false\");
        for (int i = 0; i < 3; i++) {
            //给所有线程下达命令为false
            Registry.INSTANCE.saveKey(\"job\" + i + \"_isNo\", false);
        }
        //停止线程池
        Registry.INSTANCE.shutdown();

    }
}

打印信息:
2016-12-20 15:06:19.235 [main] INFO  t.thread.Application - 线程任务开始..
 2016-12-20 15:06:19.240 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:19.240 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:19.240 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:21.242 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:21.242 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:21.242 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:23.242 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:23.242 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:23.243 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:25.243 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:25.243 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:25.243 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:27.244 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:27.245 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:27.245 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:29.241 [main] INFO  t.thread.Application - 修改所有任务间隔时间为1秒
 2016-12-20 15:06:29.245 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:29.246 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:29.246 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:30.245 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:30.246 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:30.246 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:31.246 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:31.247 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:31.247 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:32.247 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:32.248 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:32.249 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:33.247 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:33.248 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:33.250 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:34.248 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:34.248 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:34.251 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:35.249 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:35.249 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:35.251 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:36.249 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:36.249 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:36.252 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:37.249 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:37.250 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:37.252 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:38.249 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2正在执行。。。
 2016-12-20 15:06:38.250 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3正在执行。。。
 2016-12-20 15:06:38.252 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1正在执行。。。
 2016-12-20 15:06:39.241 [main] INFO  t.thread.Application - 给所有线程下达命令为false
 2016-12-20 15:06:39.251 [pool-1-thread-2] INFO  top.thread.thead.Job - pool-1-thread-2停止
 2016-12-20 15:06:39.254 [pool-1-thread-3] INFO  top.thread.thead.Job - pool-1-thread-3停止
 2016-12-20 15:06:39.254 [pool-1-thread-1] INFO  top.thread.thead.Job - pool-1-thread-1停止

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值