> 文章列表 > springboot同一个类中调用@Async不会启用异步线程

springboot同一个类中调用@Async不会启用异步线程

springboot同一个类中调用@Async不会启用异步线程

问题是这样在一个service类中有一个方法异步关闭操作,另外一个删除方法可以调用这个关闭操作,但是会卡住,并不能进入新线程关闭。代码如下:

@Slf4j
@Service("gAutoCmdService")
public class GAutoCmdServiceImpl extends ServiceImpl<GAutoCmdMapper, GAutoCmd> implements GAutoCmdService {@Overridepublic boolean removeById(Serializable id) {GAutoCmd gAutoCmd = getById(id);if(gAutoCmd.getStatus()==1){//运行中,停止任务 再删除stopTask(new GAutoCmd(gAutoCmd), false);}return super.removeById(id);}@Override@Asyncpublic void stopTask(GAutoCmd gAutoCmd, boolean restart){Thread.sleep(60000);}}

现在有一个办法可以解决,就是不知道有没有更好的办法,使用applicationContext,代码如下:

@Slf4j
@Service("gAutoCmdService")
public class GAutoCmdServiceImpl extends ServiceImpl<GAutoCmdMapper, GAutoCmd> implements GAutoCmdService {@Autowiredprivate ApplicationContext applicationContext;@Overridepublic boolean removeById(Serializable id) {GAutoCmd gAutoCmd = getById(id);if(gAutoCmd.getStatus()==1){//运行中,停止任务 再删除GAutoCmdService self = (GAutoCmdService)applicationContext.getBean("gAutoCmdService");self.stopTask(new GAutoCmd(gAutoCmd), false);}return super.removeById(id);}@Override@Asyncpublic void stopTask(GAutoCmd gAutoCmd, boolean restart){Thread.sleep(60000);}}

有没有更好的办法啊,