mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-18 03:25:38 +00:00
Seriously implemented all the threading for item and subset saving/loading/filtering/sorting
This commit is contained in:
1 parent
46c842337a
commit
a6746d80a2
5 files changed
+292
-218
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package codechicken.nei;
|
||||
|
||||
public abstract class RestartableTask
|
||||
{
|
||||
public final String name;
|
||||
private Thread thread;
|
||||
private volatile boolean restart;
|
||||
private volatile boolean stopped;
|
||||
|
||||
private ThreadOperationTimer timer;
|
||||
|
||||
public RestartableTask(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private void start() {
|
||||
thread = new Thread(name) {
|
||||
@Override
|
||||
public void run() {
|
||||
do {
|
||||
while(stopped)
|
||||
try { Thread.sleep(1); } catch (InterruptedException ignored) {}
|
||||
if(!restart)
|
||||
execute();
|
||||
} while(!finish());
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private synchronized boolean finish() {
|
||||
if(restart) {
|
||||
restart = false;
|
||||
return false;
|
||||
} else {
|
||||
clearTasks();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void clearTasks() {
|
||||
thread = null;
|
||||
timer = null;
|
||||
}
|
||||
|
||||
public synchronized void restart() {
|
||||
if(thread != null) {
|
||||
stopped = false;
|
||||
restart = true;
|
||||
}
|
||||
else
|
||||
start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops execution until restart is called. If a thread is running, the restart flag will be set, and the thread will be paused once the execute method exits.
|
||||
*/
|
||||
public synchronized void stop() {
|
||||
if(thread != null) {
|
||||
stopped = true;
|
||||
restart = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean interrupted() {
|
||||
return restart;
|
||||
}
|
||||
|
||||
public synchronized ThreadOperationTimer getTimer(int timeout) {
|
||||
if (timer == null) timer = ThreadOperationTimer.start(thread, timeout);
|
||||
return timer;
|
||||
}
|
||||
|
||||
public abstract void execute();
|
||||
}
|
||||
Reference in new issue
Block a user