java多线程实现Runnable接口时传参问题

多线程启动多个视频流

package org.jeecg.modules.xjgl.service;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.GetBeanUtil;

@Slf4j
public class ExecuteTSCommandThread implements Runnable {

    //    @Autowired
//    private IYsXjglService ysXjglService;
    IYsXjglService ysXjglService = (IYsXjglService) GetBeanUtil.getBean(IYsXjglService.class);

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    @Override
    public void run() {
        log.info("主键ID=={}", id);
        boolean bln = ysXjglService.executeCommand(id);
        log.info("多线程==>" + Thread.currentThread().getName() + "执行" + bln);
    }
}

启动多线程

    /**
     * 定时启动处于停用状态的相机
     */
    @Override
    public void startXj() {
        //查询所有处于停用状态的相机。
        QueryWrapper<YsXjgl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("xj_ffmpeg", 1).eq("del_flag", 0);
        List<YsXjgl> xjglList = ysXjglMapper.selectList(queryWrapper);
        //有几个停用的相机启动几个相机的进程
        for (YsXjgl xjgl : xjglList) {
            ExecuteTSCommandThread tsCommandThread = new ExecuteTSCommandThread();
            tsCommandThread.setId(xjgl.getId());
            new Thread(tsCommandThread).start();
        }
    }

多线程时Autowired自动注入问题

package org.jeecg.common.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GetBeanUtil implements ApplicationContextAware {


	private static ApplicationContext applicationContext = null;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		GetBeanUtil.applicationContext = applicationContext;
	}

	public static Object getBeanByName(String beanName) {
		if (applicationContext == null) {
			return null;
		}
		return applicationContext.getBean(beanName);
	}

	public static <T> T getBean(Class<T> type) {
		return applicationContext.getBean(type);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值