/** * Returns an array containing the existing client connections. This can be used by concrete * subclasses to implement messages that do something with each connection (e.g. kill it, send a * message to it etc.). Remember that after this array is obtained, some clients in this migth * disconnect. New clients can also connect, these later will not appear in the array. * * @return an array of <code>Thread</code> containing <code>ConnectionToClient</code> instances. */ public final synchronized Thread[] getClientConnections() { Thread[] clientThreadList = new Thread[clientThreadGroup.activeCount()]; clientThreadGroup.enumerate(clientThreadList); return clientThreadList; }
private Runner getRunner() { try { return (Runner) threadpool.pop(); } catch (EmptyStackException empty) { if (runners.activeCount() > 255) throw new RuntimeException("System overload"); return new Runner(); } }
public static void main(String args[]) { try { ServerSocket serv = new ServerSocket(8080); System.out.println("showserver created at port 8080."); ThreadGroup workers = new ThreadGroup("WebWorkers"); while (true) { if (workers.activeCount() < maxCon) { System.out.println("Hay " + workers.activeCount() + " hebras corriendo."); Socket conn = serv.accept(); Thread hs = new Thread(workers, new HebraServ(conn)); hs.start(); } else { try { Thread.sleep(500); } catch (Exception ex) { } } } } catch (IOException e) { System.err.println(e); } }
public static boolean kill() { int j = 0; Thread meMySelfI = Thread.currentThread(); do { /* iter across the thread group, killing all members. */ shutdown = true; Thread list[] = new Thread[tg.activeCount()]; // get all members of the group (including submembers) int i = tg.enumerate(list); // no members means that we have gracefully suceeded if (i == 0) return true; // if some of the threads do IO during the shut down they will // need time to accomplish the IO. So, I give it to 'em // after the first attempt. if (j > 0) try { meMySelfI.sleep(500); } catch (Exception e) { } ; // try to shudown each thread in the group while (i-- > 0) { FlickrFtpd tftp = (FlickrFtpd) list[i]; tftp.interrupt(); // first, do it politely meMySelfI.yield(); // give 'em time to respond tftp.forceClose(); // second, use a big hammer meMySelfI.yield(); // give 'em time to respond } } while (j++ <= 3); return false; }
/** * Counts the number of clients currently connected. * * @return the number of clients currently connected. */ public final int getNumberOfClients() { return clientThreadGroup.activeCount(); }