Skip to content

Commit f222f43

Browse files
committed
working on updating concurrent array api
1 parent bc857ac commit f222f43

23 files changed

Lines changed: 632 additions & 573 deletions

rlib-common/src/main/java/com/ss/rlib/common/concurrent/deadlock/DeadLockDetector.java

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.ss.rlib.common.util.array.Array;
77
import com.ss.rlib.common.util.array.ArrayFactory;
88
import com.ss.rlib.common.util.array.ConcurrentArray;
9+
import lombok.Getter;
910
import org.jetbrains.annotations.NotNull;
1011
import 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.forEach(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;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.BiConsumer;
6+
7+
@FunctionalInterface
8+
public interface NotNullBiConsumer<T, U> extends BiConsumer<T, U> {
9+
10+
@Override
11+
void accept(@NotNull T first, @NotNull U second);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.BiFunction;
6+
7+
@FunctionalInterface
8+
public interface NotNullBiFunction<T, U, R> extends BiFunction<T, U, R> {
9+
10+
@Override
11+
@NotNull R apply(@NotNull T first, @NotNull U second);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.BiPredicate;
6+
7+
@FunctionalInterface
8+
public interface NotNullBiPredicate<T, U> extends BiPredicate<T, U> {
9+
10+
@Override
11+
boolean test(@NotNull T first, @NotNull U second);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.Consumer;
6+
7+
@FunctionalInterface
8+
public interface NotNullConsumer<T> extends Consumer<T> {
9+
10+
@Override
11+
void accept(@NotNull T object);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.Function;
6+
7+
@FunctionalInterface
8+
public interface NotNullFunction<T, R> extends Function<T, R> {
9+
10+
@Override
11+
@NotNull R apply(@NotNull T object);
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.util.function.BiFunction;
7+
8+
@FunctionalInterface
9+
public interface NotNullNullableBiFunction<T, U, R> extends BiFunction<T, U, R> {
10+
11+
@Override
12+
@Nullable R apply(@NotNull T first, @NotNull U second);
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.util.function.Function;
7+
8+
@FunctionalInterface
9+
public interface NotNullNullableFunction<T, R> extends Function<T, R> {
10+
11+
@Override
12+
@Nullable R apply(@NotNull T object);
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
/**
6+
* @author JavaSaBr
7+
*/
8+
@FunctionalInterface
9+
public interface NotNullObjectIntPredicate<T> extends ObjectIntPredicate<T> {
10+
11+
@Override
12+
boolean test(@NotNull T first, int second);
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.Predicate;
6+
7+
@FunctionalInterface
8+
public interface NotNullPredicate<T> extends Predicate<T> {
9+
10+
@Override
11+
boolean test(@NotNull T object);
12+
}

0 commit comments

Comments
 (0)