22
33import java .lang .reflect .InvocationTargetException ;
44import java .lang .reflect .Method ;
5+ import java .util .List ;
56import java .util .Set ;
7+ import java .util .concurrent .ThreadFactory ;
68import java .util .concurrent .ThreadPoolExecutor ;
79import java .util .concurrent .TimeUnit ;
810
1113 */
1214public class EventExecutor {
1315
14- private final EventHandlerList eventHandlerList = new BasicEventHandlerList () ;
16+ private final EventHandlerList eventHandlerList ;
1517
16- private final EventHandlerObjectMap eventHandlerObjectMap = new HashHandlerObjectMap () ;
18+ private final EventHandlerObjectMap eventHandlerObjectMap ;
1719
1820 private final ThreadPoolExecutor threadPoolExecutor ;
1921
22+ private Thread .UncaughtExceptionHandler exceptionHandler = null ;
23+
24+ /**
25+ * 构造一个EventExecutor.
26+ * @param threadPoolExecutor 事件线程池, 线程池将用于执行Handler中的EventMethod.
27+ */
28+ public EventExecutor (ThreadPoolExecutor threadPoolExecutor ){
29+ this (threadPoolExecutor , null , null );
30+ }
31+
2032 /**
21- * 构造一个EventExecutor
22- * @param threadPoolExecutor 设置线程池, 线程池将用于执行Handler中的EventMethod.
33+ * 构造一个EventExecutor.
34+ * @param threadPoolExecutor 事件线程池, 线程池将用于执行Handler中的EventMethod.
35+ * @param eventHandlerList 事件列表, 用于存储EventObject对应EventHandlerMethod.
36+ * @param eventHandlerObjectMap EventHandlerObject存储, 用于存储EventHandler对象.
2337 */
24- public EventExecutor (ThreadPoolExecutor threadPoolExecutor ) {
38+ public EventExecutor (ThreadPoolExecutor threadPoolExecutor ,
39+ EventHandlerList eventHandlerList ,
40+ EventHandlerObjectMap eventHandlerObjectMap ) {
2541 this .threadPoolExecutor = threadPoolExecutor ;
42+ final ThreadFactory threadFactory = this .threadPoolExecutor .getThreadFactory ();
43+ this .threadPoolExecutor .setThreadFactory (r -> {
44+ Thread newThread = threadFactory .newThread (r );
45+ newThread .setUncaughtExceptionHandler ((t , e ) -> {
46+ if (this .exceptionHandler != null ){
47+ this .exceptionHandler .uncaughtException (t , e );
48+ }
49+ });
50+ return newThread ;
51+ });
52+ this .eventHandlerList = eventHandlerList != null ? eventHandlerList : new BasicEventHandlerList ();
53+ this .eventHandlerObjectMap = eventHandlerObjectMap != null ? eventHandlerObjectMap : new HashHandlerObjectMap ();
2654 }
2755
2856 /**
29- * 添加EventHandler
57+ * 添加EventHandler.
3058 * @param handler EventHandler对象
3159 * @throws IllegalAccessException 当EventHandler内方法不为Public时可能会抛出,
3260 * 是否抛出由{@link EventHandlerList}所使用的实现决定
@@ -36,6 +64,15 @@ public void addHandler(EventHandler handler) throws IllegalAccessException {
3664 eventHandlerList .addEventHandler (handler .getClass ());
3765 }
3866
67+ /**
68+ * 删除EventHandler
69+ * @param handler 要删除的EventHandler对象
70+ */
71+ public void removeHandler (EventHandler handler ){
72+ eventHandlerObjectMap .removeHandlerObject (handler );
73+ eventHandlerList .removeEventHandler (handler );
74+ }
75+
3976 /**
4077 * 投递事件.
4178 * 事件将会以方法为单位执行.
@@ -51,21 +88,68 @@ public void executor(final EventObject eventObject){
5188 threadPoolExecutor .execute (() -> {
5289 try {
5390 method .invoke (handler , eventObject );
54- } catch (IllegalAccessException | InvocationTargetException e ) {
55- e .printStackTrace ();
91+ } catch (IllegalAccessException e ) {
92+ throw new RuntimeException (e );
93+ } catch (InvocationTargetException e ){
94+ throw new RuntimeException (e .getCause () == null ? e : e .getCause ());
5695 }
5796 });
5897 });
5998 }
6099
61100 /**
62- * 等待任务线程池内的任务执行完成 .
101+ * 等待线程池内任务执行完成并关闭线程池 .
63102 * @param timeout 时间
64103 * @param unit 时间单位
65- * @throws InterruptedException 当发生死锁时抛出
104+ * @return 如果在设置时间内线程池任务执行完成并关闭返回true, 否则返回false.
105+ * @throws InterruptedException 当发生中断时抛出
106+ */
107+ public boolean awaitEventExecutor (long timeout , TimeUnit unit ) throws InterruptedException {
108+ return threadPoolExecutor .awaitTermination (timeout , unit );
109+ }
110+
111+ /**
112+ * 设置线程池异常处理类.
113+ * EventExecutor在内部经过处理,
114+ * 可以动态更改UncaughtExceptionHandler而不用担心设置后需要等线程重新建立后才生效.
115+ * @param handler 处理类对象
116+ */
117+ public void setUncaughtExceptionHandler (Thread .UncaughtExceptionHandler handler ){
118+ this .exceptionHandler = handler ;
119+ }
120+
121+ /**
122+ * 获取EventExecutor所使用的{@link ThreadPoolExecutor}
123+ * @return ThreadPoolExecutor对象
66124 */
67- public void awaitEventExecutor ( long timeout , TimeUnit unit ) throws InterruptedException {
68- threadPoolExecutor . awaitTermination ( timeout , unit ) ;
125+ public ThreadPoolExecutor getThreadPoolExecutor () {
126+ return this . threadPoolExecutor ;
69127 }
70128
129+ /**
130+ * 获取设定的UncaughtExceptionHandler
131+ * @return 返回设置的UncaughtExceptionHandler, 如无设置则返回null
132+ */
133+ public Thread .UncaughtExceptionHandler getExceptionHandler (){
134+ return this .exceptionHandler ;
135+ }
136+
137+ /**
138+ * 关闭执行器.
139+ * @param shutdownNow 是否立刻关闭
140+ * @return 如果 {@code shutdownNow}为 {@code true}, 则返回线程池内未完成的事件任务.
141+ */
142+ public List <Runnable > shutdown (boolean shutdownNow ){
143+ if (shutdownNow ) {
144+ return threadPoolExecutor .shutdownNow ();
145+ } else {
146+ threadPoolExecutor .shutdown ();
147+ return null ;
148+ }
149+ }
150+
151+ @ Override
152+ protected void finalize () {
153+ threadPoolExecutor .shutdownNow ();
154+ }
71155}
0 commit comments