Beispiel #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");
    }
  }
Beispiel #2
1
  /**
   * Start this server. If we manage an own HttpServer, then the HttpServer will be started as well.
   */
  public void start() {
    // URL as configured takes precedence
    String configUrl =
        NetworkUtil.replaceExpression(config.getJolokiaConfig().get(ConfigKey.DISCOVERY_AGENT_URL));
    jolokiaHttpHandler.start(
        lazy, configUrl != null ? configUrl : url, config.getAuthenticator() != null);

    if (httpServer != null) {
      // Starting our own server in an own thread group with a fixed name
      // so that the cleanup thread can recognize it.
      ThreadGroup threadGroup = new ThreadGroup("jolokia");
      threadGroup.setDaemon(false);

      Thread starterThread =
          new Thread(
              threadGroup,
              new Runnable() {
                @Override
                public void run() {
                  httpServer.start();
                }
              });
      starterThread.start();
      cleaner = new CleanupThread(httpServer, threadGroup);
      cleaner.start();
    }
  }
  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);
    }
  }
Beispiel #4
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());
    }
  }
Beispiel #5
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);
	}
Beispiel #6
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;
  }
Beispiel #7
0
 public static void dumptg(ThreadGroup tg, PrintWriter out) {
   if (tg == null) {
     tg = Thread.currentThread().getThreadGroup();
     while (tg.getParent() != null) tg = tg.getParent();
   }
   dumptg(tg, out, 0);
   out.flush();
 }
 public static void showThreads(PrintStream pw) {
   Thread current = Thread.currentThread();
   ThreadGroup group = current.getThreadGroup();
   while (true) {
     if (group.getParent() == null) break;
     group = group.getParent();
   }
   showThreads(pw, group, current);
 }
Beispiel #9
0
 /** Helper method for main to load tests into a thread group and start each test case. */
 private static void loadThread(String name) {
   try {
     ThreadGroup myThreadGroup = new ThreadGroup(name);
     String urlSpec = "http://54.201.122.231:8181/v4/gumball";
     // String urlSpec = "http://localhost:8080/v4/gumball" ;
     RunLoadTest test = new RunLoadTest(myThreadGroup, urlSpec);
     test.runTest(); // Run The Test in its own thread
     myThreadGroup.list();
   } catch (Exception e) {
     System.out.println("ERROR: " + e.getMessage());
   }
 }
Beispiel #10
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);
   }
 }
Beispiel #11
0
 private Runner getRunner() {
   try {
     return (Runner) threadpool.pop();
   } catch (EmptyStackException empty) {
     if (runners.activeCount() > 255) throw new RuntimeException("System overload");
     return new Runner();
   }
 }
Beispiel #12
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);
    }
  }
Beispiel #13
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;
  }
Beispiel #14
0
 /**
  * Counts the number of clients currently connected.
  *
  * @return the number of clients currently connected.
  */
 public final int getNumberOfClients() {
   return clientThreadGroup.activeCount();
 }
Beispiel #15
0
 /** In case of emergency when user want to stop the war during the xml reading */
 public static void stopAllThreads() {
   threadGroup.interrupt();
 }