Java 创建守护线程模板

package com.integral.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Test implements Runnable {

    private Thread thread;

    //执行逻辑所需的service
    @Autowired
    private  IntegralService integralService;

    @Autowired
    private Test(IntegralService integralService){
        //要注入的service
        this.integralService = integralService;
        //设置当前类为守护线程
        this.thread = new Thread(this);
        this.thread.setDaemon(true);
        this.thread.start();

    }

    @Override
    public void run() {
        //执行逻辑
        while (true){
            integralService.test();
        }
    }
}

不语
4 声望1 粉丝