1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| public class MonRunner implements Runnable {
private volatile boolean isStopped;
private volatile boolean isSuspended;
private volatile boolean isStarted;
private volatile Thread currentThread;
public MonRunner() {
}
/**
* Exécute la tâche courante
*/
public void run() {
try {
this.currentThread = Thread.currentThread();
// waiting started
this.waitStarted();
while (!this.isStopped){
try {
...
this.waitSupended();
}
catch(Exception e) {
...
}
finally {
this.waitInterval();
}
}
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
catch (Exception e){
...
}
finally {
this.currentThread = null;
}
}
/**
* Attend selon le délai définit dans le fichier de configuration "tasks-reception.xml"
*/
private void waitInterval() {
try {
Thread.sleep(this.activeTask.getInterval());
}
catch (InterruptedException e) {...
}
}
public synchronized void start() {
if(this.isStarted) {
this.isStopped = false;
}else{
this.isStarted = true;
this.currentThread = new Thread(this, this.getName());
this.currentThread.start();
}
}
/**
* Arrête la tâche courante
*/
public synchronized void stop() {
this.currentThread = new Thread(this, this.getName());
this.isStopped = true;
if (this.currentThread != null) {
this.currentThread.interrupt();
}
//System.exit(0);
}
public synchronized void suspend() {
this.isSuspended = true;
}
public synchronized void resume(){
this.isSuspended = false;
}
private void waitSupended() throws InterruptedException {
while (this.isSuspended) {
Thread.sleep(200);
}
}
private void waitStarted() throws InterruptedException {
while (this.isStarted && this.isStopped) {
Thread.sleep(200);
}
this.isStarted = false;
} |
Partager