zoukankan      html  css  js  c++  java
  • 用守护线程的方式实现定时任务

    public class ThreadTest {
        public static int  sign= 0;
    
        public static void main(String[] args) {
    
            Thread thread = new Thread(
                            new Runnable(){
                                public void run(){
                                    while(true){
                                        if(sign%10 !=0){ //这个条件可以设置为取时间判断
                                            System.out.println("守护线程保持运行");
                                            sign++;
                                        }else{
                                            System.out.println("调用定时任务");
                                            sign++;
                                        }
                                        try {
                                            Thread.sleep(5000);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }
                            );
    
            thread.setDaemon(true); // 设置为守护线程,降低线程的优先级
            thread.start();
    
            while(true){
                System.out.println("主线程保持运行");  //守护线程必须保证有主线程存在,才会生存。 
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
  • 相关阅读:
    使用Redis做MyBatis的二级缓存
    MySQL 类型转换
    mysql store procedure 存储过程
    swagger
    redis 持久化
    redis 发布/订阅 模式
    dfs模板
    二叉树
    拓扑排序
    最大公因数(辗转相除法)
  • 原文地址:https://www.cnblogs.com/heiniao/p/6547325.html
Copyright © 2011-2022 走看看