updated thread handling:

- threads are no longer pooled (this makes naming threads difficult)
- threads are now named by their causing event, which improves logging

updated calls to thread.sleep: those are now provided by new class Delayed Execution
This commit is contained in:
Stephan Richter
2021-02-28 13:03:21 +01:00
parent cabab80b5c
commit b951b948e5
47 changed files with 293 additions and 260 deletions

View File

@@ -7,9 +7,9 @@ import java.util.Map;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Connector;
import de.srsoftware.web4rail.DelayedExecution;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.moving.Train;
import de.srsoftware.web4rail.tags.Fieldset;
@@ -52,17 +52,12 @@ public abstract class Bridge extends Tile {
@Override
public Tile load(JSONObject json) {
if (json.has(COUNTERPART)) {
Application.threadPool.execute(new Thread() {
new DelayedExecution(this) {
@Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
public void execute() {
counterpart = (Bridge) plan.get(Id.from(json, COUNTERPART), false);
}
});
};
}
return super.load(json);
}

View File

@@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.DelayedExecution;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.actions.Action;
import de.srsoftware.web4rail.actions.ActionList;
@@ -36,7 +37,7 @@ public class Contact extends Tile{
private HashSet<Listener> listeners = new HashSet<Listener>();
public interface Listener{
public void fired();
public void fired(Object cause);
}
public Contact() {
@@ -52,7 +53,8 @@ public class Contact extends Tile{
boolean aborted = false;
public OffTimer() {
Application.threadPool.execute(this);
setName(Application.threadName("OffTimer("+Contact.this+")"));
start();
}
@Override
@@ -87,12 +89,10 @@ public class Contact extends Tile{
Context context = isSet(route) ? route.context().contact(this) : new Context(this);
if (isSet(route)) route.traceTrainFrom(this);
actions.fire(context);
actions.fire(context,"Contact("+addr+")");
if (isSet(route)) route.contact(this);
for (Listener listener : listeners) {
listener.fired();
}
for (Listener listener : listeners) listener.fired("Contact("+addr+")");
stream();
}
@@ -253,14 +253,13 @@ public class Contact extends Tile{
public boolean trigger(int duration) {
activate(true);
Application.threadPool.execute(new Thread() {
public void run() {
try {
sleep(duration);
activate(false);
} catch (Exception e) {}
new DelayedExecution(duration,"Contact("+Contact.this.addr+")") {
@Override
public void execute() {
activate(false);
}
});
};
return true;
}
@Override

View File

@@ -163,16 +163,19 @@ public class Switch extends Tile{
public void state(boolean newState) {
state = newState;
Application.threadPool.execute(new Runnable() {
Thread thread = new Thread() {
@Override
public void run() {
Context context = new Context(Switch.this);
if (state) {
actionsOn.fire(context);
} else actionsOff.fire(context);
actionsOn.fire(context,Switch.this);
} else actionsOff.fire(context,Switch.this);
}
});
};
thread.setName(Application.threadName(this));
thread.start();
stream();
}