본문 바로가기
C++ 200제/코딩 IT 정보

자바, 인터프리터에서 실행되는 스레드 그룹 확인하는 소스

by vicddory 2018. 12. 31.

자바, 인터프리터에서 실행되는 스레드 그룹 확인하는 소스


현재 Android Inter interpreter에서 실행되는 실행되는 스레드, 스레드 그룹들과 각 스레드별 우선 순위를 나타내주는 프로그램입니다.


이와 같이 현재 사용되고 있는 스레드와 그 목록을 알아낼 수 있다면, 프로그램 지연과 같은 전반적인 구동간의 멈춤 현상 등의 원인 유추가 가능합니다.


또한, 네트워크 프로그램 구현 시엔, "스레드 + 버퍼"를 사용할 때 보다 효과적인 관리가 가능하겠죠.


저처럼 프로그램이 죽거나, 지연이 될 때 원천적인 문제점 파악에 도움이 되네요.



자바 인터프리터 스레드 그룹[자바 인터프리터 스레드 그룹]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.*;
 
public class ThreadLister {
    // Display info about a thread.
    private static void print_thread_info(PrintStream out, Thread t, String indent) {
        if (t == nullreturn;
        out.println(indent + "Thread: " + t.getName() +
                "  Priority: " + t.getPriority() +
                (t.isDaemon()?" Daemon":""+
                (t.isAlive()?"":" Not Alive"));
    }
    
    // Display info about a thread group and its threads and groups
    private static void list_group(PrintStream out, ThreadGroup g, String indent) {
        if (g == null)
            return;
 
        int num_threads = g.activeCount();
        int num_groups = g.activeGroupCount();
 
        Thread[] threads = new Thread[num_threads];
        ThreadGroup[] groups = new ThreadGroup[num_groups];
        
        g.enumerate(threads, false);
        g.enumerate(groups, false);
        
        out.println(indent + "Thread Group: " + g.getName() + 
                "  Max Priority: " + g.getMaxPriority() +
                (g.isDaemon()?" Daemon":""));
        
        for(int i = 0; i < num_threads; i++)
            print_thread_info(out, threads[i], indent + "    ");
 
        for(int i = 0; i < num_groups; i++)
            list_group(out, groups[i], indent + "    ");
    }
    
    // Find the root thread group and list it recursively
    public static void listAllThreads(PrintStream out) {
        ThreadGroup current_thread_group;
        ThreadGroup root_thread_group;
        ThreadGroup parent;
        
        // Get the current thread group
        current_thread_group = Thread.currentThread().getThreadGroup();
        
        // Now go find the root thread group
        root_thread_group = current_thread_group;
        parent = root_thread_group.getParent();
 
        while(parent != null) {
            root_thread_group = parent;
            parent = parent.getParent();
        }
        
        // And list it, recursively
        list_group(out, root_thread_group, "");
    }
    
    public static void main(String[] args) {
        ThreadLister.listAllThreads(System.out);
    }
}
cs


자바, 인터프리터에서 실행되는 스레드 그룹 확인하는 소스

댓글