2016-03-02 90 views
2

我使用线程池产生了5个线程。现在我想知道跟踪所有这些线程和捕获每个线程的度量的最佳方式,如跟踪ThreadPoolExecutor中产生的线程java

  • 每个线程花费多少时间?
  • 每个线程使用多少内存?

我们是否有任何现成的实现为我们完成工作。任何建议都会有所帮助。

public static void main(String[] args) { 

     ThreadPoolExecutor executors = (ThreadPoolExecutor) Executors.newFixedThreadPool(5); 

     System.out.println("at executors step"); 
     List<String> getlist = getList(); 
     for (Iterator<String> itr = getlist.iterator(); itr.hasNext();) { 
      String element = (String) itr.next(); 
      String[] command = { "ssh", "hddev-c01-edge-02", "\"" + element + "\"" }; 
      System.out.println("the command is as below "); 
      System.out.println(Arrays.toString(command)); 
      System.out.println("inside the iterator"); 
      ParallelExecutor pe = new ParallelExecutor(command); 
      executors.execute(pe); 
     } 
     executors.shutdown(); 
    } 
+0

内存将很难测量,因为堆内存在所有线程之间共享并由垃圾收集器在后台进行管理。 – Thilo

+0

如果不是内存,是否可以跟踪线程? – dataEnthusiast

+0

为什么在foreach循环既简单又清晰时使用'Iterator'?附:此外,使用Java SSH库而不是运行随机外部进程。 –

回答

1

有两种方法中的ThreadPoolExecutor,你可以重写做这种内部管理的,就像一个线程多少时间了执行一个Runnable。 扩展的ThreadPoolExecutor并覆盖只有2种方法一样,

protected void beforeExecute(Thread t, Runnable r) { } 

protected void afterExecute(Runnable r, Throwable t) { } 

希望这有助于。