1 18 19 package com.martiansoftware.nailgun; 20 21 30 class NGSessionPool { 31 32 35 int poolSize = 0; 36 37 40 NGSession[] pool = null; 41 42 45 int poolEntries = 0; 46 47 50 NGServer server = null; 51 52 55 boolean done = false; 56 57 60 private Object lock = new Object (); 61 62 68 NGSessionPool(NGServer server, int maxIdle) { 69 this.server = server; 70 this.poolSize = maxIdle - 1; 71 72 pool = new NGSession[poolSize]; 73 poolEntries = 0; 74 } 75 76 80 NGSession take() { 81 NGSession result; 82 synchronized(lock) { 83 if (poolEntries == 0) { 84 result = new NGSession(this, server); 85 result.start(); 86 } else { 87 --poolEntries; 88 result = pool[poolEntries]; 89 } 90 } 91 return (result); 92 } 93 94 99 void give(NGSession session) { 100 if (done || poolEntries == poolSize) { 101 session.shutdown(); 102 } else { 103 synchronized(lock) { 104 pool[poolEntries] = session; 105 ++poolEntries; 106 } 107 } 108 } 109 110 113 void shutdown() { 114 done = true; 115 synchronized(lock) { 116 while (poolEntries > 0) { 117 take().shutdown(); 118 } 119 } 120 } 121 122 } 123 | Popular Tags |