Libraryless. Click here for Pure Java version (107L/2K/5K).
1 | !747 |
2 | |
3 | m { |
4 | static Map<Long, Thread> idToThread; |
5 | |
6 | p { |
7 | ThreadMXBean mx = ManagementFactory.getThreadMXBean(); |
8 | |
9 | // just for fun |
10 | print("Total started thread count: " + mx.getTotalStartedThreadCount()); |
11 | |
12 | boolean cpuTimeSupported = mx.isThreadCpuTimeSupported(); |
13 | print("CPU time supported: " + cpuTimeSupported); |
14 | if (!cpuTimeSupported) fail("No CPU time info available on platform."); |
15 | |
16 | long[] threads = mx.getAllThreadIds(); |
17 | |
18 | long time = now(); |
19 | L<Thread> l = getAllThreads(); |
20 | print((now()-time) + " ms for getAllThreads()"); |
21 | print(l.size() + " threads"); |
22 | |
23 | idToThread = new TreeMap(); |
24 | for (Thread t : l) |
25 | idToThread.put(t.getId(), t); |
26 | |
27 | for (long id : threads) { |
28 | long nano = mx.getThreadCpuTime(id); |
29 | long ms = nano/1000000; |
30 | Thread t = idToThread.get(id); |
31 | if (t == null) |
32 | print("? thread not found: " + id); |
33 | else |
34 | print("Thread " + quote(t.getName()) + ": CPU time = " + ms + " ms"); |
35 | } |
36 | |
37 | } |
38 | |
39 | static L<Thread> getAllThreads() { |
40 | ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); |
41 | ThreadGroup parentGroup; |
42 | while ( ( parentGroup = rootGroup.getParent() ) != null ) |
43 | rootGroup = parentGroup; |
44 | Thread[] threads = new Thread[rootGroup.activeCount()]; |
45 | int n; |
46 | while ((n = rootGroup.enumerate(threads, true)) == threads.length) threads = new Thread[threads.length*2]; |
47 | L<Thread> l = new ArrayList<Thread>(n); |
48 | for (int i = 0; i < n; i++) l.add(threads[i]); |
49 | ret l; |
50 | } |
51 | } |
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1001403 |
Snippet name: | Thread CPU time experimentation (ThreadMXBean) |
Eternal ID of this version: | #1001403/1 |
Text MD5: | f99e1606cbf6aed395304b56a48929f4 |
Transpilation MD5: | 2066c8e5b704863a91e87d12ed79d6f0 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-10-14 17:47:37 |
Source code size: | 1606 bytes / 51 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 714 / 762 |
Referenced in: | [show references] |