66import com .ss .rlib .common .util .array .Array ;
77import com .ss .rlib .common .util .array .ArrayFactory ;
88import com .ss .rlib .common .util .array .ConcurrentArray ;
9+ import lombok .Getter ;
910import org .jetbrains .annotations .NotNull ;
1011import org .jetbrains .annotations .Nullable ;
1112
@@ -29,38 +30,29 @@ public class DeadLockDetector implements Runnable {
2930 /**
3031 * The list of listeners.
3132 */
32- @ NotNull
33- private final ConcurrentArray <DeadLockListener > listeners ;
33+ private final @ Getter @ NotNull ConcurrentArray <DeadLockListener > listeners ;
3434
3535 /**
3636 * The bean with information about threads.
3737 */
38- @ NotNull
39- private final ThreadMXBean mxThread ;
38+ private final @ NotNull ThreadMXBean mxThread ;
4039
4140 /**
4241 * The scheduler.
4342 */
44- @ NotNull
45- private final ScheduledExecutorService executorService ;
43+ private final @ NotNull ScheduledExecutorService executorService ;
4644
4745 /**
4846 * The reference to a task.
4947 */
50- @ Nullable
51- private volatile ScheduledFuture <?> schedule ;
48+ private volatile @ Getter @ Nullable ScheduledFuture <?> schedule ;
5249
5350 /**
5451 * The checking interval.
5552 */
5653 private final int interval ;
5754
58- /**
59- * Instantiates a new Dead lock detector.
60- *
61- * @param interval the checking interval.
62- */
63- public DeadLockDetector (final int interval ) {
55+ public DeadLockDetector (int interval ) {
6456
6557 if (interval < 1 ) {
6658 throw new IllegalArgumentException ("negative interval." );
@@ -77,54 +69,44 @@ public DeadLockDetector(final int interval) {
7769 *
7870 * @param listener the new listener.
7971 */
80- public void addListener (@ NotNull final DeadLockListener listener ) {
81- ArrayUtils .runInWriteLock (listeners , listener , Array ::add );
82- }
83-
84- /**
85- * Gets listeners.
86- *
87- * @return the list of listeners.
88- */
89- @ NotNull
90- public ConcurrentArray <DeadLockListener > getListeners () {
91- return listeners ;
72+ public void addListener (@ NotNull DeadLockListener listener ) {
73+ listeners .runInWriteLock (listener , Array ::add );
9274 }
9375
9476 @ Override
9577 public void run () {
9678
97- final long [] threadIds = mxThread .findDeadlockedThreads ();
98- if (threadIds .length < 1 ) return ;
79+ var threadIds = mxThread .findDeadlockedThreads ();
80+
81+ if (threadIds .length < 1 ) {
82+ return ;
83+ }
84+
85+ var listeners = getListeners ();
9986
100- final ConcurrentArray < DeadLockListener > listeners = getListeners ();
87+ for ( var id : threadIds ) {
10188
102- for ( final long id : threadIds ) {
89+ var info = mxThread . getThreadInfo ( id );
10390
104- final ThreadInfo info = mxThread .getThreadInfo (id );
105- if (listeners .isEmpty ()) continue ;
91+ if (listeners .isEmpty ()) {
92+ continue ;
93+ }
10694
107- ArrayUtils .runInReadLock (listeners , info ,
108- (deadLockListeners , threadInfo ) ->
109- deadLockListeners .forEach (threadInfo , DeadLockListener ::onDetected ));
95+ listeners .runInReadLock (info , (list , inf ) -> list .forEachR (inf , DeadLockListener ::onDetected ));
11096
11197 LOGGER .warning ("DeadLock detected! : " + info );
11298 }
11399 }
114100
115- /**
116- * @return the reference to a task.
117- */
118- @ Nullable
119- private ScheduledFuture <?> getSchedule () {
120- return schedule ;
121- }
122-
123101 /**
124102 * Start.
125103 */
126104 public synchronized void start () {
127- if (schedule != null ) return ;
105+
106+ if (schedule != null ) {
107+ return ;
108+ }
109+
128110 schedule = executorService .scheduleAtFixedRate (this , interval , interval , TimeUnit .MILLISECONDS );
129111 }
130112
@@ -133,8 +115,12 @@ public synchronized void start() {
133115 */
134116 public synchronized void stop () {
135117
136- final ScheduledFuture <?> schedule = getSchedule ();
137- if (schedule == null ) return ;
118+ var schedule = getSchedule ();
119+
120+ if (schedule == null ) {
121+ return ;
122+ }
123+
138124 schedule .cancel (false );
139125
140126 this .schedule = null ;
0 commit comments