comparison src/ch/ethz/ssh2/util/TimeoutService.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
18 * <p> 18 * <p>
19 * Implemented having large scale programs in mind: if you open many concurrent SSH connections 19 * Implemented having large scale programs in mind: if you open many concurrent SSH connections
20 * that rely on timeouts, then there will be only one timeout thread. Once all timeouts 20 * that rely on timeouts, then there will be only one timeout thread. Once all timeouts
21 * have expired/are cancelled, the thread will (sooner or later) exit. 21 * have expired/are cancelled, the thread will (sooner or later) exit.
22 * Only after new timeouts arrive a new thread (singleton) will be instantiated. 22 * Only after new timeouts arrive a new thread (singleton) will be instantiated.
23 * 23 *
24 * @author Christian Plattner 24 * @author Christian Plattner
25 * @version $Id: TimeoutService.java 89 2014-04-07 14:36:24Z dkocher@sudo.ch $ 25 * @version $Id: TimeoutService.java 89 2014-04-07 14:36:24Z dkocher@sudo.ch $
26 */ 26 */
27 public class TimeoutService 27 public class TimeoutService {
28 { 28 private static final Logger log = Logger.getLogger(TimeoutService.class);
29 private static final Logger log = Logger.getLogger(TimeoutService.class);
30 29
31 public static class TimeoutToken 30 public static class TimeoutToken {
32 { 31 private long runTime;
33 private long runTime; 32 private Runnable handler;
34 private Runnable handler;
35 33
36 private TimeoutToken(long runTime, Runnable handler) 34 private TimeoutToken(long runTime, Runnable handler) {
37 { 35 this.runTime = runTime;
38 this.runTime = runTime; 36 this.handler = handler;
39 this.handler = handler; 37 }
40 } 38 }
41 }
42 39
43 private static class TimeoutThread extends Thread 40 private static class TimeoutThread extends Thread {
44 { 41 @Override
45 @Override 42 public void run() {
46 public void run() 43 synchronized (todolist) {
47 { 44 while (true) {
48 synchronized (todolist) 45 if (todolist.size() == 0) {
49 { 46 timeoutThread = null;
50 while (true) 47 return;
51 { 48 }
52 if (todolist.size() == 0)
53 {
54 timeoutThread = null;
55 return;
56 }
57 49
58 long now = System.currentTimeMillis(); 50 long now = System.currentTimeMillis();
51 TimeoutToken tt = todolist.getFirst();
59 52
60 TimeoutToken tt = todolist.getFirst(); 53 if (tt.runTime > now) {
54 /* Not ready yet, sleep a little bit */
55 try {
56 todolist.wait(tt.runTime - now);
57 }
58 catch (InterruptedException ignored) {
59 }
61 60
62 if (tt.runTime > now) 61 /* We cannot simply go on, since it could be that the token
63 { 62 * was removed (cancelled) or another one has been inserted in
64 /* Not ready yet, sleep a little bit */ 63 * the meantime.
64 */
65 continue;
66 }
65 67
66 try 68 todolist.removeFirst();
67 {
68 todolist.wait(tt.runTime - now);
69 }
70 catch (InterruptedException ignored)
71 {
72 }
73 69
74 /* We cannot simply go on, since it could be that the token 70 try {
75 * was removed (cancelled) or another one has been inserted in 71 tt.handler.run();
76 * the meantime. 72 }
77 */ 73 catch (Exception e) {
74 StringWriter sw = new StringWriter();
75 e.printStackTrace(new PrintWriter(sw));
76 log.warning("Exeception in Timeout handler:" + e.getMessage() + "(" + sw.toString() + ")");
77 }
78 }
79 }
80 }
81 }
78 82
79 continue; 83 /* The list object is also used for locking purposes */
80 } 84 private static final LinkedList<TimeoutToken> todolist = new LinkedList<TimeoutService.TimeoutToken>();
81 85
82 todolist.removeFirst(); 86 private static Thread timeoutThread = null;
83 87
84 try 88 /**
85 { 89 * It is assumed that the passed handler will not execute for a long time.
86 tt.handler.run(); 90 *
87 } 91 * @param runTime
88 catch (Exception e) 92 * @param handler
89 { 93 * @return a TimeoutToken that can be used to cancel the timeout.
90 StringWriter sw = new StringWriter(); 94 */
91 e.printStackTrace(new PrintWriter(sw)); 95 public static TimeoutToken addTimeoutHandler(long runTime, Runnable handler) {
92 log.warning("Exeception in Timeout handler:" + e.getMessage() + "(" + sw.toString() + ")"); 96 TimeoutToken token = new TimeoutToken(runTime, handler);
93 }
94 }
95 }
96 }
97 }
98 97
99 /* The list object is also used for locking purposes */ 98 synchronized (todolist) {
100 private static final LinkedList<TimeoutToken> todolist = new LinkedList<TimeoutService.TimeoutToken>(); 99 todolist.add(token);
100 Collections.sort(todolist, new Comparator<TimeoutToken>() {
101 public int compare(TimeoutToken o1, TimeoutToken o2) {
102 if (o1.runTime > o2.runTime)
103 return 1;
101 104
102 private static Thread timeoutThread = null; 105 if (o1.runTime == o2.runTime)
106 return 0;
103 107
104 /** 108 return -1;
105 * It is assumed that the passed handler will not execute for a long time. 109 }
106 * 110 });
107 * @param runTime
108 * @param handler
109 * @return a TimeoutToken that can be used to cancel the timeout.
110 */
111 public static TimeoutToken addTimeoutHandler(long runTime, Runnable handler)
112 {
113 TimeoutToken token = new TimeoutToken(runTime, handler);
114 111
115 synchronized (todolist) 112 if (timeoutThread != null)
116 { 113 timeoutThread.interrupt();
117 todolist.add(token); 114 else {
115 timeoutThread = new TimeoutThread();
116 timeoutThread.setDaemon(true);
117 timeoutThread.start();
118 }
119 }
118 120
119 Collections.sort(todolist, new Comparator<TimeoutToken>() 121 return token;
120 { 122 }
121 public int compare(TimeoutToken o1, TimeoutToken o2)
122 {
123 if (o1.runTime > o2.runTime)
124 return 1;
125 if (o1.runTime == o2.runTime)
126 return 0;
127 return -1;
128 }
129 });
130 123
131 if (timeoutThread != null) 124 public static void cancelTimeoutHandler(TimeoutToken token) {
132 timeoutThread.interrupt(); 125 synchronized (todolist) {
133 else 126 todolist.remove(token);
134 {
135 timeoutThread = new TimeoutThread();
136 timeoutThread.setDaemon(true);
137 timeoutThread.start();
138 }
139 }
140 127
141 return token; 128 if (timeoutThread != null)
142 } 129 timeoutThread.interrupt();
143 130 }
144 public static void cancelTimeoutHandler(TimeoutToken token) 131 }
145 {
146 synchronized (todolist)
147 {
148 todolist.remove(token);
149
150 if (timeoutThread != null)
151 timeoutThread.interrupt();
152 }
153 }
154 132
155 } 133 }