@@ -163,10 +163,12 @@ public void close(SQLiteProcessor processor) {
163163 @ Override
164164 public void run () {
165165 try {
166- for (; !isStopped () || this .processors .size () > 0 ;) {
166+ SlotAllocator <SQLiteProcessor > processors = this .processors ;
167+ for (; !isStopped () || processors .size () > 0 ;) {
167168 long timeout = minSelectTimeout ();
168169 int n ;
169170 if (timeout < 0L ) {
171+ processIdle ();
170172 n = this .selector .select ();
171173 } else if (timeout == 0L ) {
172174 n = this .selector .selectNow ();
@@ -202,6 +204,7 @@ protected void processQueues(long runNanos) throws IllegalStateException {
202204 Selector selector = this .selector ;
203205 long deadNano = System .nanoTime () + runNanos ;
204206 // Q1: procQueue
207+ SlotAllocator <SQLiteProcessor > processors = this .processors ;
205208 for (;;) {
206209 if (runNanos > 0L && System .nanoTime () > deadNano ) {
207210 return ;
@@ -216,7 +219,7 @@ protected void processQueues(long runNanos) throws IllegalStateException {
216219 p .setSelector (selector );
217220 p .setWorker (this );
218221 p .setName (this .name + "-" + p .getName ());
219- if (this . processors .size () >= this .maxConns ) {
222+ if (processors .size () >= this .maxConns ) {
220223 p .tooManyConns ();
221224 p .stop ();
222225 p .enableWrite ();
@@ -230,7 +233,7 @@ protected void processQueues(long runNanos) throws IllegalStateException {
230233 }
231234 this .procsLock .lock ();
232235 try {
233- final int slot = this . processors .allocate (p );
236+ final int slot = processors .allocate (p );
234237 if (slot == -1 ) {
235238 throw new IllegalStateException ("Processor allocator full" );
236239 }
@@ -284,7 +287,23 @@ protected void processQueues(long runNanos) throws IllegalStateException {
284287 }
285288 }
286289
290+ protected void processIdle () {
291+ SlotAllocator <SQLiteProcessor > processors = this .processors ;
292+ this .procsLock .lock ();
293+ try {
294+ for (int i = 0 , n = processors .maxSlot (); i < n ; ++i ) {
295+ SQLiteProcessor p = processors .get (i );
296+ if (p != null && p .isStopped ()) {
297+ p .write ();
298+ }
299+ }
300+ } finally {
301+ this .procsLock .unlock ();
302+ }
303+ }
304+
287305 protected void processIO () {
306+ final Thread currThead = Thread .currentThread ();
288307 Iterator <SelectionKey > keys = this .selector .selectedKeys ().iterator ();
289308 for (; keys .hasNext (); keys .remove ()) {
290309 SelectionKey key = keys .next ();
@@ -297,18 +316,18 @@ protected void processIO() {
297316 if (key .isWritable ()) {
298317 try {
299318 p = (SQLiteProcessor )key .attachment ();
300- Thread . currentThread () .setName (p .getName ());
319+ currThead .setName (p .getName ());
301320 p .write ();
302321 } finally {
303- Thread . currentThread () .setName (this .name );
322+ currThead .setName (this .name );
304323 }
305324 } else if (key .isReadable ()) {
306325 try {
307326 p = (SQLiteProcessor )key .attachment ();
308- Thread . currentThread () .setName (p .getName ());
327+ currThead .setName (p .getName ());
309328 p .read ();
310329 } finally {
311- Thread . currentThread () .setName (this .name );
330+ currThead .setName (this .name );
312331 }
313332 } else {
314333 key .cancel ();
@@ -411,10 +430,11 @@ protected long minSelectTimeout() {
411430 }
412431
413432 SQLiteProcessor getProcessor (int pid ) {
433+ SlotAllocator <SQLiteProcessor > processors = this .processors ;
414434 this .procsLock .lock ();
415435 try {
416- for (int i = 0 , n = this . processors .maxSlot (); i < n ; ++i ) {
417- SQLiteProcessor p = this . processors .get (i );
436+ for (int i = 0 , n = processors .maxSlot (); i < n ; ++i ) {
437+ SQLiteProcessor p = processors .get (i );
418438 if (p != null && p .getId () == pid ) {
419439 return p ;
420440 }
@@ -426,12 +446,13 @@ SQLiteProcessor getProcessor(int pid) {
426446 }
427447
428448 public List <SQLiteProcessorState > getProcessorStates (final SQLiteProcessor processor ) {
449+ SlotAllocator <SQLiteProcessor > processors = this .processors ;
429450 this .procsLock .lock ();
430451 try {
431452 List <SQLiteProcessorState > states = new ArrayList <>();
432453 final User user = processor .getUser ();
433- for (int i = 0 , n = this . processors .maxSlot (); i < n ; ++i ) {
434- final SQLiteProcessor p = this . processors .get (i );
454+ for (int i = 0 , n = processors .maxSlot (); i < n ; ++i ) {
455+ final SQLiteProcessor p = processors .get (i );
435456 if (p == null ) {
436457 continue ;
437458 }
0 commit comments