Checkstyle fix

This commit is contained in:
Jindra Petřík
2025-04-13 14:13:27 +02:00
parent 859a2f8bdb
commit b1c5f9d0c6
85 changed files with 2486 additions and 2340 deletions

View File

@@ -31,14 +31,13 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
/*private static final Map<Thread, CancellableWorker> threadWorkers = Collections.synchronizedMap(new WeakHashMap<>());
/*private static final Map<Thread, CancellableWorker> threadWorkers = Collections.synchronizedMap(new WeakHashMap<>());
private static final Map<Thread, CancellableWorker> parentThreadWorkers = Collections.synchronizedMap(new WeakHashMap<>());
private static final Map<Thread, Thread> threadToParentThread = Collections.synchronizedMap(new WeakHashMap<>());
*/
*/
/**
* Cancellable worker.
@@ -55,57 +54,55 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
private FutureTask<T> future;
private List<CancellableWorker> subWorkers = Collections.synchronizedList(new ArrayList<>());
private static final Map<Thread, CancellableWorker> thread2Worker = Collections.synchronizedMap(new WeakHashMap<>());
private static final Map<Thread, CancellableWorker> thread2Worker = Collections.synchronizedMap(new WeakHashMap<>());
private Thread parentThread;
private Thread thread;
private String name;
private CancellableWorker parentWorker;
private boolean canceled = false;
private List<Runnable> cancelListeners = new ArrayList<>();
public Thread getThread() {
return thread;
}
}
public static void assignThreadToWorker(Thread t, CancellableWorker w) {
if (w == null) {
return;
}
thread2Worker.put(t, w);
}
public static CancellableWorker getCurrent() {
Thread t = Thread.currentThread();
CancellableWorker w = thread2Worker.get(t);
return w;
}
public void addCancelListener(Runnable listener) {
cancelListeners.add(listener);
}
public void removeCancelListener(Runnable listener) {
cancelListeners.remove(listener);
}
public static boolean isInterrupted() {
Thread t = Thread.currentThread();
if (t.isInterrupted()) {
return true;
}
CancellableWorker w = thread2Worker.get(t);
if (w != null) {
CancellableWorker w = thread2Worker.get(t);
if (w != null) {
while (w != null) {
t = w.thread;
if (t != null && t.isInterrupted()) {
@@ -115,26 +112,27 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
return true;
}
w = w.parentWorker;
}
}
}
return false;
}
/**
* Constructor.
*
* @param name Identifier of action
*/
public CancellableWorker(String name) {
super();
this.name = name;
this.name = name;
Callable<T> callable = new Callable<T>() {
@Override
public T call() throws Exception {
thread = Thread.currentThread();
thread = Thread.currentThread();
thread2Worker.put(thread, CancellableWorker.this);
if (isInterrupted()) {
throw new InterruptedException();
}
}
return doInBackground();
}
};
@@ -149,6 +147,7 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
/**
* Do in background.
*
* @return Result
* @throws Exception On error
*/
@@ -188,25 +187,24 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
}
@Override
public final boolean cancel(boolean mayInterruptIfRunning) {
public final boolean cancel(boolean mayInterruptIfRunning) {
canceled = true;
boolean r = future.cancel(mayInterruptIfRunning);
List<CancellableWorker> sw = new ArrayList<>(subWorkers);
for (CancellableWorker w : sw) {
w.cancel(mayInterruptIfRunning);
}
if (r) {
List<Runnable> cls = new ArrayList<>(cancelListeners);
}
if (r) {
List<Runnable> cls = new ArrayList<>(cancelListeners);
for (Runnable listener : cls) {
listener.run();
}
workerCancelled();
}
}
return r;
}
@@ -234,17 +232,15 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
@Override
public final T get(long timeout, TimeUnit unit) throws InterruptedException,
ExecutionException, TimeoutException {
ExecutionException, TimeoutException {
return future.get(timeout, unit);
}
@Override
public String toString() {
return "[CancellableWorker \"" + name + "\" on thread " + thread +", subworkers: " + subWorkers.size() + ", " + (canceled ? " canceled" : "") + ", " + (parentWorker != null ? "HASPARENT" : "NOPARENT");
return "[CancellableWorker \"" + name + "\" on thread " + thread + ", subworkers: " + subWorkers.size() + ", " + (canceled ? " canceled" : "") + ", " + (parentWorker != null ? "HASPARENT" : "NOPARENT");
}
private void workerDone() {
if (thread != null && thread2Worker.get(thread) == this) {
//thread2Worker.remove(thread);
@@ -255,6 +251,7 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
/**
* Calls a callable with a timeout.
*
* @param name Name
* @param c Callable
* @param timeout Timeout
@@ -265,7 +262,7 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
* @throws ExecutionException On execution error
* @throws TimeoutException On timeout
*/
public static <T> T call(String name, final Callable<T> c, long timeout, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
public static <T> T call(String name, final Callable<T> c, long timeout, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
CancellableWorker<T> worker = new CancellableWorker<T>(name) {
@Override
@@ -279,7 +276,7 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
} finally {
worker.cancel(true);
}
}
}
/**
* Cancels all background threads.
@@ -295,7 +292,7 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
}
}
}
public static void cancelThread(Thread t) {
List<CancellableWorker> ws = new ArrayList<>(workers);
for (CancellableWorker w : ws) {
@@ -304,7 +301,7 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
w.cancel(true);
}
}
}
}
}
/**
@@ -316,7 +313,7 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
w.free();
}
}
@SuppressWarnings("unchecked")
private void printTree(List<String> out) {
out.add("worker " + name + " on thread " + thread);
@@ -324,32 +321,32 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
for (CancellableWorker sw : sws) {
List<String> subout = new ArrayList<>();
sw.printTree(subout);
for (String s:subout) {
for (String s : subout) {
out.add("-" + s);
}
}
}
public void printTree() {
System.err.println("=======================");
List<String> out = new ArrayList<>();
printTree(out);
for (String s : out) {
System.err.println(s);
}
System.err.println("/=======================");
}
public static void printAllWorkers() {
List<CancellableWorker> aw = new ArrayList<>(workers);
System.err.println("====ALL WORKERS ====");
for (CancellableWorker w : aw) {
System.err.println("" + w);
}
System.err.println("/====================");
}
public void printTree() {
System.err.println("=======================");
List<String> out = new ArrayList<>();
printTree(out);
for (String s:out) {
System.err.println(s);
}
System.err.println("/=======================");
}
public void startWorkerMonitor() {
Thread monit = new Thread() {
@Override
@@ -364,8 +361,8 @@ public abstract class CancellableWorker<T> implements RunnableFuture<T> {
return;
}
}
}
}
};
monit.start();
monit.start();
}
}