private synchronized ThreadPool getPool() { if (pool == null) pool = new ThreadPool (1, 10); return pool; }
public boolean process (ISOSource source, ISOMsg m) { if (pool == null) pool = new ThreadPool (1, 10); pool.execute (new Process (source, m)); return true; } }
public void supervise () { Thread[] t = new Thread[maxPoolSize]; int cnt = enumerate (t); for (int i=0; i<cnt; i++) if (t[i] instanceof PooledThread) ((PooledThread) t[i]).supervise(); }
public void dump (PrintStream p, String indent) { String inner = indent + " "; p.println (indent + "<thread-pool name=\""+getName()+"\">"); if (!pool.ready()) p.println (inner + "<closed/>"); p.println (inner + "<jobs>" + getJobCount() + "</jobs>"); p.println (inner + "<size>" + getPoolSize() + "</size>"); p.println (inner + "<max>" + getMaxPoolSize() + "</max>"); p.println (inner + "<active>" + getActiveCount() + "</active>"); p.println (inner + "<idle>" + getIdleCount() + "</idle>"); p.println (inner + "<active>" + getActiveCount() + "</active>"); p.println (inner + "<pending>" + getPendingCount() + "</pending>"); p.println (indent + "</thread-pool>"); }
@Test public void testConstructor() throws Throwable { ThreadPool threadPool = new ThreadPool(-1, 1); assertEquals("threadPool.getJobCount()", 0, threadPool.getJobCount()); assertEquals("threadPool.getPoolSize()", 1, threadPool.getPoolSize()); assertEquals("threadPool.getMaxPoolSize()", 1, threadPool.getMaxPoolSize()); }
@Test @SuppressWarnings("deprecation") public void testIssue196 () { ThreadPool pool = new ThreadPool(10, 20, "thepool"); int available = pool.getAvailableCount(); int active = pool.getActiveCount(); long end = System.currentTimeMillis() + 10000L; // run for 10 seconds top int i = 0; while (active >= 0 && System.currentTimeMillis() < end) { while (available > 0) { pool.execute(() -> { }); available = pool.getAvailableCount(); } available = pool.getAvailableCount(); active = pool.getActiveCount(); } assertTrue ("Active should be >= 0 but it is " + active, active >= 0); }
@Test public void testRunNames() throws Throwable { ThreadPool threadPool = new ThreadPool(1, 2); threadPool.execute(new TestTask()); ISOUtil.sleep(20); threadPool.execute(new TestTask()); ISOUtil.sleep(20); Thread[] tl = new Thread[threadPool.activeCount()]; threadPool.enumerate(tl); for (Thread t :tl ) assertThat(t.getName(), new Matches("ThreadPool.PooledThread-\\d+-(running|idle)")); }
@Ignore @Test public void testConcurrentThreadAllocation() throws Throwable { ThreadPool pool = new ThreadPool(1, 200, "Test-ThreadPool"); Server server = new Server(pool); Thread serverThread = new Thread(server); serverThread.start(); serverThread.join(15000); assertEquals("pool.getActiveCount()", 100, pool.getActiveCount()); synchronized (server) { server.notifyAll(); } pool.close(); }
while (!shutdown) { try { if (pool.getAvailableCount() <= 0) { try { serverSocket.close(); for (int i=0; pool.getIdleCount() == 0; i++) { if (shutdown) { break serverLoop; channels.put (channel.getName(), wr); channels.put (LAST, wr); pool.execute (createSession(channel)); setChanged (); notifyObservers (this);
public boolean process (ISOSource source, ISOMsg m) { pool.execute (new Processor (source, m)); return false; // let other listeners do their work too } }
pool = new ThreadPool (minSessions ,maxSessions, getName() + "-ThreadPool"); pool.setLogger (log.getLogger(), getName() + ".pool");
@Override public int getJobCount () { return pool.getJobCount(); } @Override
@Override public int getMaxPoolSize () { return pool.getMaxPoolSize(); } @Override
@Override public int getPoolSize () { return pool.getPoolSize(); } @Override
@Override public int getPendingCount () { return pool.getPendingCount(); } public int getActiveConnections () {
public int getActiveConnections () { return pool.getActiveCount(); }
private void shutdownServer () { try { if (serverSocket != null) { serverSocket.close (); fireEvent(new ISOServerShutdownEvent(this)); } if (pool != null) { pool.close(); } } catch (IOException e) { fireEvent(new ISOServerShutdownEvent(this)); Logger.log (new LogEvent (this, "shutdown", e)); } } private void shutdownChannels () {
@Test public void testConstructor1() throws Throwable { ThreadPool threadPool = new ThreadPool(-2, -1); assertEquals("threadPool.getJobCount()", 0, threadPool.getJobCount()); assertEquals("threadPool.getPoolSize()", 1, threadPool.getPoolSize()); assertEquals("threadPool.getMaxPoolSize()", 100, threadPool.getMaxPoolSize()); }
@Override public void run() { for (int i = 0; i < 100; i++) { pool.execute(new Job(i, this)); } } }
@Test public void testConstructor3() throws Throwable { ThreadPool threadPool = new ThreadPool(1, 0); assertEquals("threadPool.getJobCount()", 0, threadPool.getJobCount()); assertEquals("threadPool.getPoolSize()", 1, threadPool.getPoolSize()); assertEquals("threadPool.getMaxPoolSize()", 100, threadPool.getMaxPoolSize()); }