通过观察者模式对线程状态进行监控示例

public interface LifeCycleListener {

	void onEvent(AbservableRunnable.RunnableEvent event);
}
public enum RunnableState {

	RUNNING, ERROR, DONE
}

 

public abstract class AbservableRunnable implements Runnable{

	final protected LifeCycleListener listener;
	
	public AbservableRunnable(LifeCycleListener listener) {
		this.listener = listener;
	}
	
	protected void notifyChange(final RunnableEvent event) {
		listener.onEvent(event);
	}
	
	public static class RunnableEvent {
		private final RunnableState state;
		private final Thread thread;
		private final Throwable cause;
		
		public RunnableEvent(RunnableState state, Thread thread, Throwable cause) {
			this.state = state;
			this.thread = thread;
			this.cause = cause;
		}

		public RunnableState getState() {
			return state;
		}

		public Thread getThread() {
			return thread;
		}

		public Throwable getCause() {
			return cause;
		}
	}
}
public class ThreadLifeCycleListener implements LifeCycleListener {
	
	private final Object LOCK = new Object();
	
	public void query(List<String> ids) {
		if (ids == null || ids.isEmpty()) {
			return ;
		}
		ids.stream().forEach(id->new Thread(new AbservableRunnable(this) {
			@Override
			public void run() {
				try {
					notifyChange(new RunnableEvent(RunnableState.RUNNING, Thread.currentThread(), null));
					System.out.println("Query for the id : " + id);
					Thread.sleep(1000);
					notifyChange(new RunnableEvent(RunnableState.DONE, Thread.currentThread(), null));
				} catch (Exception e) {
					notifyChange(new RunnableEvent(RunnableState.ERROR, Thread.currentThread(), e));
				}
			}
		}, id).start());
	}

	@Override
	public void onEvent(RunnableEvent event) {
		synchronized (LOCK) {
			System.out.println("The runnable 【" + event.getThread().getName() + "】, data changed and state is 【" + event.getState() + "】");
			if (event.getCause() != null) {
				System.out.println("The runnable 【" + event.getThread().getName() + "】 process failed!");
				event.getCause().printStackTrace();
			}
		}
	}
}
public class ThreadLifeCycleMain {

	public static void main(String[] args) {
		new ThreadLifeCycleListener().query(Arrays.asList("100", "200"));
	}
}

 运行结果:

The runnable 【100】, data changed and state is 【RUNNING】
The runnable 【200】, data changed and state is 【RUNNING】
Query for the id : 200
Query for the id : 100
The runnable 【100】, data changed and state is 【DONE】
The runnable 【200】, data changed and state is 【DONE】

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值