mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 06:33:34 +02:00
Möglichkeit beliebige Tasks zu timen
Die ZM kann nun beliebige Runnables mit einem Zeitdelay versehen ausführen. Mitteles `scheduleTask(Runnable, int)` wird das angegebene Runnable nach der angegebene Anzahl Millisekunden ausgeführt. Tasks werden immer am Ende eines Frames ausgeführt.
This commit is contained in:
@@ -12,8 +12,7 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Hauptklasse der Zeichenmaschine.
|
||||
@@ -1090,6 +1089,37 @@ public class Zeichenmaschine extends Constants {
|
||||
// Intentionally left blank
|
||||
}
|
||||
|
||||
/*
|
||||
* Task scheduling
|
||||
*/
|
||||
private DelayQueue<DelayedTask> taskQueue = new DelayQueue<>();
|
||||
|
||||
public void scheduleTask( Runnable runnable, int delay ) {
|
||||
taskQueue.add(new DelayedTask(delay, runnable));
|
||||
}
|
||||
|
||||
public void scheduleTask( Runnable runnable, int delay, boolean concurrent ) {
|
||||
DelayedTask task = new DelayedTask(delay, runnable);
|
||||
task.concurrent = concurrent;
|
||||
taskQueue.add(task);
|
||||
}
|
||||
|
||||
private void runTasks() {
|
||||
synchronized( taskQueue ) {
|
||||
DelayedTask task = taskQueue.poll();
|
||||
while( task != null ) {
|
||||
if( task.concurrent ) {
|
||||
SwingUtilities.invokeLater(task.runnable);
|
||||
// new Thread(task.runnable).start();
|
||||
} else {
|
||||
task.runnable.run();
|
||||
}
|
||||
|
||||
task = taskQueue.poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Mouse handling
|
||||
*/
|
||||
@@ -1324,6 +1354,8 @@ public class Zeichenmaschine extends Constants {
|
||||
dispatchEvents();
|
||||
}
|
||||
|
||||
runTasks();
|
||||
|
||||
// delta time in ns
|
||||
long afterTime = System.nanoTime();
|
||||
long dt = afterTime - beforeTime;
|
||||
@@ -1380,4 +1412,28 @@ public class Zeichenmaschine extends Constants {
|
||||
|
||||
}
|
||||
|
||||
class DelayedTask implements Delayed {
|
||||
long startTime; // in ms
|
||||
Runnable runnable;
|
||||
|
||||
boolean concurrent = false;
|
||||
|
||||
public DelayedTask( int delay, Runnable runnable ) {
|
||||
this.startTime = System.currentTimeMillis() + delay;
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getDelay( TimeUnit unit ) {
|
||||
int diff = (int)(startTime - System.currentTimeMillis());
|
||||
return unit.convert(diff, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo( Delayed o ) {
|
||||
return (int) (startTime- ((DelayedTask)o).startTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user