Пример #1
1
  public static void dumpThreads(ThreadGroup threadGroup, String indent) {
    Thread[] threadList = new Thread[threadGroup.activeCount()];

    threadGroup.enumerate(threadList);

    for (int i = 0; i < threadList.length; i++) {

      Thread t = threadList[i];

      if (t != null) {

        out(
            indent
                .concat("active thread = ")
                .concat(t.toString())
                .concat(", daemon = ")
                .concat(String.valueOf(t.isDaemon())));
      }
    }

    if (threadGroup.getParent() != null) {

      dumpThreads(threadGroup.getParent(), indent + "\t");
    }
  }
Пример #2
1
  private static void showThreads(PrintStream pw, ThreadGroup g, Thread current) {
    int nthreads = g.activeCount();
    pw.println("\nThread Group = " + g.getName() + " activeCount= " + nthreads);
    Thread[] tarray = new Thread[nthreads];
    int n = g.enumerate(tarray, false);

    for (int i = 0; i < n; i++) {
      Thread thread = tarray[i];
      ClassLoader loader = thread.getContextClassLoader();
      String loaderName = (loader == null) ? "Default" : loader.getClass().getName();
      Thread.State state = thread.getState();
      long id = thread.getId();
      pw.print("   " + id + " " + thread.getName() + " " + state + " " + loaderName);
      if (thread == current) pw.println(" **** CURRENT ***");
      else pw.println();
    }

    int ngroups = g.activeGroupCount();
    ThreadGroup[] garray = new ThreadGroup[ngroups];
    int ng = g.enumerate(garray, false);
    for (int i = 0; i < ng; i++) {
      ThreadGroup nested = garray[i];
      showThreads(pw, nested, current);
    }
  }
Пример #3
1
  public static void killAWTThreads(ThreadGroup threadGroup) {
    Thread[] threadList = new Thread[threadGroup.activeCount()];

    threadGroup.enumerate(threadList);

    for (int i = 0; i < threadList.length; i++) {

      Thread t = threadList[i];

      if (t != null) {

        String name = t.getName();

        if (name.startsWith("AWT")) {

          out("Interrupting thread '".concat(t.toString()).concat("'"));

          t.interrupt();
        }
      }
    }

    if (threadGroup.getParent() != null) {

      killAWTThreads(threadGroup.getParent());
    }
  }
Пример #4
0
	public static void main(String[] args) throws Exception{
		//free和use和total均为KB 
		long free=0; 
		long use=0; 
		long total=0;  
		int kb=1024;

		Runtime rt=Runtime.getRuntime();
		total=rt.totalMemory();
		free=rt.freeMemory();
		use=total-free;
		
		System.out.println("系统内存已用的空间为:"+use/kb+" MB");
		System.out.println("系统内存的空闲空间为:"+free/kb+" MB");
		System.out.println("系统总内存空间为:"+total/kb+" MB");   

		OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
		
		long physicalFree=osmxb.getFreePhysicalMemorySize()/kb;
		long physicalTotal=osmxb.getTotalPhysicalMemorySize()/kb;
		long physicalUse=physicalTotal-physicalFree;
		String os=System.getProperty("os.name");
		System.out.println("操作系统的版本:"+os);
		System.out.println("系统物理内存已用的空间为:"+physicalFree+" MB");
		System.out.println("系统物理内存的空闲空间为:"+physicalUse+" MB");
		System.out.println("总物理内存:"+physicalTotal+" MB");

		// 获得线程总数 
        	ThreadGroup parentThread;
        	for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent())
			;
		int totalThread = parentThread.activeCount();

		System.out.println("获得线程总数:"+totalThread);
	}
Пример #5
0
  /**
   * 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;
  }
Пример #6
0
 private Runner getRunner() {
   try {
     return (Runner) threadpool.pop();
   } catch (EmptyStackException empty) {
     if (runners.activeCount() > 255) throw new RuntimeException("System overload");
     return new Runner();
   }
 }
Пример #7
0
  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);
    }
  }
Пример #8
0
 private static void dumptg(ThreadGroup tg, PrintWriter out, int indent) {
   for (int o = 0; o < indent; o++) out.print("\t");
   out.println("G: \"" + tg.getName() + "\"");
   Thread[] ths = new Thread[tg.activeCount() * 2];
   ThreadGroup[] tgs = new ThreadGroup[tg.activeGroupCount() * 2];
   int nt = tg.enumerate(ths, false);
   int ng = tg.enumerate(tgs, false);
   for (int i = 0; i < nt; i++) {
     Thread ct = ths[i];
     for (int o = 0; o < indent + 1; o++) out.print("\t");
     out.println("T: \"" + ct.getName() + "\"");
   }
   for (int i = 0; i < ng; i++) {
     ThreadGroup cg = tgs[i];
     dumptg(cg, out, indent + 1);
   }
 }
Пример #9
0
  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;
  }
Пример #10
0
 /**
  * Counts the number of clients currently connected.
  *
  * @return the number of clients currently connected.
  */
 public final int getNumberOfClients() {
   return clientThreadGroup.activeCount();
 }