如何在不影响主流程的情况下,完成消息的推送。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xue_mind/article/details/78978904

说白了就是在主流程中重新启动一个线程,两个进程互不相干。

例子:

一,新建一个监听,继承Thread,重新该方法:

public abstract class AbstractThreadListener extends Thread {
	
	
	@Override
	public void run() {
		try {
			checkParam();
			doBusiness();
		} catch (Throwable t) {
			System.out.println("error>>run listener failed, listener:" + this.getClass().getName(), t);
		}
	}

	protected abstract void checkParam();
	protected abstract void doBusiness();
}
二,写一个实现类,继承监听,来完成你想完成的任务:


public class ViolationListener extends AbstractThreadListener {
	
	private String type;

	public ViolationListener (String type) {
		this.type = type;
	}

	
	protected void checkParam() {//用来做判断
		if (this.param == null) {
			throw new RuntimeException("ViolationListener ,param is empty.");
		}
	}

	
	protected void doBusiness() {
              //这里写你要实现的任务
             System.out.println("Inside doBusiness() method.");
	}
}
三,获取对象,启动线程,

new ViolationListener ("DELETE").start();


猜你喜欢

转载自blog.csdn.net/xue_mind/article/details/78978904