- boolean need_monitor() {
- return reader.opened() && !callbacks.isEmpty();
- }
-
- void maybe_stop_monitor() {
- if (!need_monitor() && monitor_thread != null) {
- monitor_thread.interrupt();
- try {
- monitor_thread.join();
- } catch (InterruptedException e) {
- } finally {
- monitor_thread = null;
- }
- }
- }
-
- void maybe_start_monitor() {
- if (need_monitor() && monitor_thread == null) {
- monitor_thread = new Thread(this);
- monitor_thread.start();
- }
- }
-
- public void monitor(AltosSerialMonitor monitor) {
- synchronized(callbacks) {
- callbacks.add(monitor);
- maybe_start_monitor();
- }
- }
-
-
- public void unmonitor(AltosSerialMonitor monitor) {
- synchronized(callbacks) {
- callbacks.remove(monitor);
- maybe_stop_monitor();
- }
- }