在某些场景下我们需要为一些比较重(资源过大,处理时间很长)的事情起一个新线程,与此同时,我们需要观察这写线程的每个阶段的情况,便于做出处理,一直去询问线程你正在处于什么阶段这样的设计肯定是不合理的,那么这个时候观察者设计模式就派上用处了。
线程生命周期的观察者
只要是实现了我规定的接口的类就可以是一个线程生命周期的观察者。
/**
* @author guoxingyong
* @Description
*/
public interface ThreadLifeCycleObserver {
//当线程开始运作的时候进行的回调
void onStart();
//当线程正常结束的时候进行的回调
void onEnd();
//当线程错误结束的时候进行的回调
void onError(Throwable throwable);
}
我们这里给予两个测试的观察者。
public class ThreadLifeCycleObserverImpl1 implements ThreadLifeCycleObserver {
public ThreadLifeCycleObserverImpl1(ThreadLifeCycleSubject threadLifeCycleSubject) {
threadLifeCycleSubject.register(this);
}
@Override
public void onStart() {
System.out.println("ThreadLifeCycleObserverImpl1观察到"+Thread.currentThread()+"启动了--->do something");
}
@Override
public void onEnd() {
System.out.println("ThreadLifeCycleObserverImpl1观察到"+Thread.currentThread()+"结束了--->do something");
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
System.out.println("ThreadLifeCycleObserverImpl1观察到"+Thread.currentThread()+"异常了--->do something");
}
}
public class ThreadLifeCycleObserverImpl2 implements ThreadLifeCycleObserver {
public ThreadLifeCycleObserverImpl2(ThreadLifeCycleSubject threadLifeCycleSubject) {
threadLifeCycleSubject.register(this);
}
@Override
public void onStart() {
System.out.println("ThreadLifeCycleObserverImpl2观察到"+Thread.currentThread()+"启动了--->do something");
}
@Override
public void onEnd() {
System.out.println("ThreadLifeCycleObserverImpl2观察到"+Thread.currentThread()+"结束了--->do something");
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
System.out.println("ThreadLifeCycleObserverImpl2观察到"+Thread.currentThread()+"异常了--->do something");
}
}
通知这些对此感兴趣的类就需要一个subject
public class ThreadLifeCycleSubject {
/**
* 作为共享资源,所以需要保证线程安全
*/
private LinkedBlockingQueue<ThreadLifeCycleObserver> lifeCycleObservers=new LinkedBlockingQueue<>();
//对于是这个接口的类添加进来,说明他对于需要被监控的线程关心,希望知道他的生命周期
public void register(ThreadLifeCycleObserver ThreadLifeCycleObserver){
try {
lifeCycleObservers.put(ThreadLifeCycleObserver);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//移除掉已经对线程周期不关心的类
public void unsubscribe(ThreadLifeCycleObserver ThreadLifeCycleObserver){
lifeCycleObservers.remove(ThreadLifeCycleObserver);
}
public void notifyStart(){
lifeCycleObservers.forEach(ThreadLifeCycleObserver::onStart);
}
public void notifyEnd(){
lifeCycleObservers.forEach(ThreadLifeCycleObserver::onEnd);
}
public void notifyError(Throwable throwable){
lifeCycleObservers.forEach(threadLifeCycleObserver -> threadLifeCycleObserver.onError(throwable));
}
}
- 做出测试模拟
public static void main(String[] args) {
ThreadLifeCycleSubject threadLifeCycleSubject = new ThreadLifeCycleSubject();
new ThreadLifeCycleObserverImpl1(threadLifeCycleSubject);
new ThreadLifeCycleObserverImpl2(threadLifeCycleSubject);
ThreadDoSomething(threadLifeCycleSubject);
}
private static void ThreadDoSomething(ThreadLifeCycleSubject threadLifeCycleSubject) {
IntStream.range(0, 10).forEach(i ->
new Thread(() -> {
try {
threadLifeCycleSubject.notifyStart();
//doSomething
if(i==5){
throw new NullPointerException();
}
threadLifeCycleSubject.notifyEnd();
} catch (Exception e) {
threadLifeCycleSubject.notifyError(e);
}
}, "test thread" + i).start()
);
}
2.测试结果
java.lang.NullPointerException
ThreadLifeCycleObserverImpl1观察到Thread[test thread4,5,main]启动了--->do something
at com.shell.rule.listener.Test.lambda$null$0(Test.java:25)
ThreadLifeCycleObserverImpl1观察到Thread[test thread7,5,main]启动了--->do something
at java.lang.Thread.run(Thread.java:748)
ThreadLifeCycleObserverImpl1观察到Thread[test thread6,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread8,5,main]启动了--->do something
java.lang.NullPointerException
ThreadLifeCycleObserverImpl2观察到Thread[test thread6,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread5,5,main]启动了--->do something
at com.shell.rule.listener.Test.lambda$null$0(Test.java:25)
ThreadLifeCycleObserverImpl2观察到Thread[test thread8,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread9,5,main]启动了--->do something
at java.lang.Thread.run(Thread.java:748)
ThreadLifeCycleObserverImpl1观察到Thread[test thread2,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread1,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread0,5,main]启动了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread7,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread3,5,main]启动了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread4,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread6,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread3,5,main]启动了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread0,5,main]启动了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread1,5,main]启动了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread2,5,main]启动了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread9,5,main]启动了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread5,5,main]启动了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread9,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread2,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread7,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread1,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread0,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread3,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread6,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread8,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread4,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread3,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread0,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread1,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread7,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread2,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread9,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread8,5,main]结束了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread4,5,main]结束了--->do something
ThreadLifeCycleObserverImpl1观察到Thread[test thread5,5,main]异常了--->do something
ThreadLifeCycleObserverImpl2观察到Thread[test thread5,5,main]异常了--->do something
观察者观察到了所启动的十个线程的生命周期。并按照自己的想法对于每一个阶段做出了处理。