@@ -35,9 +35,9 @@ public abstract class NioClient {
3535 /** Packet logger, if available. */
3636 private static PacketLogger packetLogger = null ;
3737
38- private static Runnable timeoutTask ;
39- private static Runnable registrationsTask ;
40- private static Runnable closeTask ;
38+ private static final Runnable [] TIMEOUT_TASKS = new Runnable [ 2 ] ;
39+ private static final Runnable [] REGISTRATIONS_TASKS = new Runnable [ 2 ] ;
40+ private static final Runnable [] CLOSE_TASKS = new Runnable [ 2 ] ;
4141 private static Thread selectorThread ;
4242 private static Thread closeThread ;
4343 private static volatile Selector selector ;
@@ -85,7 +85,7 @@ private static void close(boolean fromHook) {
8585 }
8686
8787 try {
88- closeTask . run ( );
88+ runTasks ( CLOSE_TASKS );
8989 } catch (Exception e ) {
9090 log .warn ("Failed to execute shutdown task, ignoring and continuing close" , e );
9191 }
@@ -121,11 +121,11 @@ static void runSelector() {
121121 while (run ) {
122122 try {
123123 if (selector .select (timeout ) == 0 ) {
124- timeoutTask . run ( );
124+ runTasks ( TIMEOUT_TASKS );
125125 }
126126
127127 if (run ) {
128- registrationsTask . run ( );
128+ runTasks ( REGISTRATIONS_TASKS );
129129 processReadyKeys ();
130130 }
131131 } catch (IOException e ) {
@@ -137,16 +137,35 @@ static void runSelector() {
137137 log .debug ("dnsjava NIO selector thread stopped" );
138138 }
139139
140- static void setTimeoutTask (Runnable r ) {
141- timeoutTask = r ;
140+ static synchronized void setTimeoutTask (Runnable r , boolean isTcpClient ) {
141+ addTask ( TIMEOUT_TASKS , r , isTcpClient ) ;
142142 }
143143
144- static void setRegistrationsTask (Runnable r ) {
145- registrationsTask = r ;
144+ static synchronized void setRegistrationsTask (Runnable r , boolean isTcpClient ) {
145+ addTask ( REGISTRATIONS_TASKS , r , isTcpClient ) ;
146146 }
147147
148- static void setCloseTask (Runnable r ) {
149- closeTask = r ;
148+ static synchronized void setCloseTask (Runnable r , boolean isTcpClient ) {
149+ addTask (CLOSE_TASKS , r , isTcpClient );
150+ }
151+
152+ private static void addTask (Runnable [] closeTasks , Runnable r , boolean isTcpClient ) {
153+ if (isTcpClient ) {
154+ closeTasks [0 ] = r ;
155+ } else {
156+ closeTasks [1 ] = r ;
157+ }
158+ }
159+
160+ private static synchronized void runTasks (Runnable [] runnables ) {
161+ Runnable r0 = runnables [0 ];
162+ if (r0 != null ) {
163+ r0 .run ();
164+ }
165+ Runnable r1 = runnables [1 ];
166+ if (r1 != null ) {
167+ r1 .run ();
168+ }
150169 }
151170
152171 private static void processReadyKeys () {
0 commit comments