01: /*
02: * This is free software, licensed under the Gnu Public License (GPL)
03: * get a copy from <http://www.gnu.org/licenses/gpl.html>
04: *
05: * author: Henner Zeller <H.Zeller@acm.org>
06: */
07: package henplus.commands;
08:
09: import henplus.HenPlus;
10: import henplus.view.util.CancelWriter;
11:
12: /**
13: * After arming, this runnable will display the current time
14: * after some timeout. Used in long running SQL-statements
15: * to show a) how long it took so far b) keep terminal
16: * sessions open that are otherwise being closed by some
17: * firewalls :-)
18: *
19: * @author hzeller
20: * @version $Revision: 1.1 $
21: */
22: public class LongRunningTimeDisplay implements Runnable {
23: private final long _startTimeDisplayAfter;
24: private final String _message;
25: private final CancelWriter _timeDisplay;
26: private long _lastArmTime;
27: private volatile boolean _running;
28: private volatile boolean _armed;
29:
30: public LongRunningTimeDisplay(String msg, long showAfter) {
31: _startTimeDisplayAfter = showAfter;
32: _message = msg;
33: _running = true;
34: _armed = false;
35: _timeDisplay = new CancelWriter(HenPlus.msg());
36: }
37:
38: public synchronized void arm() {
39: _lastArmTime = System.currentTimeMillis();
40: _armed = true;
41: notify();
42: }
43:
44: public synchronized void disarm() {
45: if (_armed) {
46: _armed = false;
47: _timeDisplay.cancel();
48: notify();
49: }
50: }
51:
52: public synchronized void stopThread() {
53: _running = false;
54: notify();
55: }
56:
57: public synchronized void run() {
58: try {
59: for (;;) {
60: while (_running && !_armed) {
61: wait();
62: }
63: if (!_running)
64: return;
65: long startDisplayAt = _lastArmTime
66: + _startTimeDisplayAfter;
67: while (_armed
68: && System.currentTimeMillis() < startDisplayAt) {
69: wait(300);
70: }
71: while (_armed) {
72: long totalTime = System.currentTimeMillis()
73: - _lastArmTime;
74: totalTime -= totalTime % 1000; // full seconds.
75: String time = TimeRenderer.renderTime(totalTime);
76: _timeDisplay.cancel();
77: _timeDisplay.print(_message + " " + time);
78: wait(5000);
79: }
80: _timeDisplay.cancel();
81: }
82: } catch (InterruptedException e) {
83: return;
84: }
85: }
86: }
|