How to check the number of currently running threads in Java?

后端 未结 5 1295
耶瑟儿~
耶瑟儿~ 2020-12-08 05:47

I\'m looking for a way to see the number of currently running threads

  1. Through Windows first
  2. Programmatically
5条回答
  •  温柔的废话
    2020-12-08 06:07

    In response to your following comment

    In the following piece of code: while(resultSet.next()) { name=resultSet.getString("hName"); MyRunnable worker = new MyRunnable(hName); threadExecutor.execute( worker ); } . My thread pool has size of 10. I need to make sure that my program working correctly with multi-threadings & want to check how many threads are running at a certain moment. How can I do this?

    to another answer, I suggest that you profile your code with JVisualVM and check if your thread pool is working as it should. The reason behind this suggestion is that then you don't have to bother with all the other housekeeping threads that JVM manages. Besides what you want to do is why tools like JVisualVM are made for.

    If you are new to profiling Java programs, JVisualVM lets you see what goes on under the hood while you are running your code. You can see the Heap, GC activity, inspect the threads running/waiting any sample/profile your cpu or memory usage. There are quite a few plugins as well.

提交回复
热议问题