2014-06-29 20:31:37 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Objects;
|
2023-09-25 00:35:24 +02:00
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class ThreadOperationTimer extends Thread {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public static class TimeoutException extends RuntimeException {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2024-08-29 20:07:14 +02:00
|
|
|
private static final long serialVersionUID = -8621458005263888185L;
|
2014-06-29 20:31:37 +10:00
|
|
|
public final Object operation;
|
|
|
|
|
|
|
|
|
|
public TimeoutException(String msg, Object op) {
|
|
|
|
|
super(msg);
|
|
|
|
|
operation = op;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 20:19:16 -08:00
|
|
|
private final Thread thread;
|
2014-06-29 20:31:37 +10:00
|
|
|
private Object operation;
|
|
|
|
|
private long opTime;
|
|
|
|
|
private long limit;
|
|
|
|
|
|
|
|
|
|
private ThreadOperationTimer(Thread thread, int limit) {
|
2022-06-24 12:32:13 +01:00
|
|
|
super(thread.getName() + " Operation Timer");
|
2014-06-29 20:31:37 +10:00
|
|
|
this.thread = thread;
|
|
|
|
|
this.limit = limit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void setLimit(int limit) {
|
|
|
|
|
this.limit = limit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
|
reset(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void reset(Object op) {
|
|
|
|
|
operation = op;
|
|
|
|
|
opTime = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void update(Object op) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!Objects.equal(operation, op)) {
|
2014-06-29 20:31:37 +10:00
|
|
|
operation = op;
|
|
|
|
|
opTime = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (FMLCommonHandler.instance().findContainerFor("NotEnoughItems").getVersion().contains("$")) return; // don't
|
|
|
|
|
// run
|
|
|
|
|
// this
|
|
|
|
|
// thread
|
|
|
|
|
// in a
|
|
|
|
|
// source
|
|
|
|
|
// environment
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
while (thread.isAlive()) {
|
|
|
|
|
synchronized (this) {
|
2021-08-14 13:42:11 -06:00
|
|
|
if (operation != null && System.currentTimeMillis() - opTime > limit) {
|
|
|
|
|
// Thread.stop(Throwable) is hard-deprecated and IntelliJ complains about it.
|
2022-06-24 12:32:13 +01:00
|
|
|
// thread.stop(new TimeoutException("Operation took too long", operation));
|
2021-08-14 13:42:11 -06:00
|
|
|
thread.stop();
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(50);
|
2022-06-24 12:32:13 +01:00
|
|
|
} catch (InterruptedException ie) {}
|
2014-06-29 20:31:37 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ThreadOperationTimer start(Thread thread, int limit) {
|
|
|
|
|
ThreadOperationTimer t = new ThreadOperationTimer(thread, limit);
|
|
|
|
|
t.start();
|
|
|
|
|
return t;
|
|
|
|
|
}
|
2022-06-24 12:32:13 +01:00
|
|
|
}
|