-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
44 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/src/main/java/de/serviceflow/frankenstein/ExecutorThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package de.serviceflow.frankenstein; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
import java.util.concurrent.Executor; | ||
|
||
public class ExecutorThread implements Executor { | ||
final Queue<Runnable> tasks = new ArrayDeque<Runnable>(); | ||
Runnable active; | ||
boolean isAlive = true; | ||
final Thread thread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
System.out.println("ExecutorThread running"); | ||
while (isAlive) { | ||
scheduleNext(); | ||
Thread.sleep(10L); | ||
} | ||
System.out.println("ExecutorThread shudown"); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
|
||
private ExecutorThread() { | ||
thread.start(); | ||
} | ||
|
||
private void stop() { | ||
isAlive=false; | ||
} | ||
|
||
private static ExecutorThread instance = null; | ||
|
||
public static synchronized ExecutorThread getInstance() { | ||
if (instance==null) | ||
instance = new ExecutorThread(); | ||
return instance; | ||
} | ||
|
||
public static void shutdown() { | ||
if (instance!=null) | ||
instance.stop(); | ||
} | ||
|
||
public synchronized void execute(final Runnable r) { | ||
tasks.offer(r); | ||
} | ||
|
||
protected synchronized void scheduleNext() { | ||
if ((active = tasks.poll()) != null) { | ||
active.run(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters