본문 바로가기

명사 美 비격식 (무리 중에서) 아주 뛰어난[눈에 띄는] 사람[것]

JAVA

보안상의 이유로 도입된 스레드 그룹: 서로 관련된 스레드를 그룹으로!

스레드 그룹은 서로 관련된 스레드를 그룹으로 다루기 위한 것.

스레드그룹은 보안상의 이유로 도입된 개념

자신이 속한 스레드 그룹이나 하위 스레드 그룹이 아니라면 다른 스레드 그룹의 스레드는 변경할 수 없다.

 

ThreadGroup(Spring name)

지정된 이름을 가진 새로운 스레드 그룹을 만듭니다. 

이 생성자를 사용하면 새로운 스레드 그룹을 만들고 해당 그룹의 이름을 설정할 수 있습니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
System.out.println("Thread group created with name: " + group.getName());

출력 결과 예시:Thread group created with name: ExampleGroup

 

ThreadGroup(ThreadGroup parent, String name)

지정된 부모 스레드 그룹과 이름을 가진 새로운 스레드 그룹을 만듭니다. 이 생성자를 사용하면 지정된 부모 스레드 그룹 내에서 새로운 스레드 그룹을 만들고 그룹의 이름을 설정할 수 있습니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup = new ThreadGroup(parentGroup, "ChildGroup");
System.out.println("Child thread group created with name: " + childGroup.getName());

출력 결과 예시:Child thread group created with name: ChildGroup

 

int activeCount()

현재 스레드 그룹 내에서 활성 상태인 스레드의 근사치를 반환합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");

Thread thread1 = new Thread(group, () -> {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
});
thread1.start();

Thread thread2 = new Thread(group, () -> {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
});
thread2.start();

int activeThreadCount = group.activeCount();
System.out.println("Active threads in the group: " + activeThreadCount);

출력 결과 예시:Active threads in the group: 2

 

int activeGroupCount()

현재 스레드 그룹 내에서 활성 상태인 하위 스레드 그룹의 근사치를 반환합니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup1 = new ThreadGroup(parentGroup, "ChildGroup1");
ThreadGroup childGroup2 = new ThreadGroup(parentGroup, "ChildGroup2");

int activeGroupCount = parentGroup.activeGroupCount();
System.out.println("Active subgroups in the parent group: " + activeGroupCount);

출력 결과 예시:Active subgroups in the parent group: 2



void checkAccess()

현재 스레드가 스레드 그룹에 접근할 수 있는지 확인합니다. 만약 접근할 수 없는 경우 `SecurityException`을 발생시킵니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
group.checkAccess();
System.out.println("Access granted.");

출력 결과 예시: (접근 가능한 경우)Access granted.
**예시 코드 (접근 불가능한 경우):
ThreadGroup group = new ThreadGroup("ExampleGroup");
group.destroy(); // ThreadGroup 파괴

group.checkAccess(); // SecurityException 발생
System.out.println("Access granted.");

출력 결과 예시: (접근 불가능한 경우)Exception in thread "main" java.lang.SecurityException: ThreadGroup destroyed
    at java.base/java.lang.ThreadGroup.checkAccess(ThreadGroup.java:330)
    at com.example.Main.main(Main.java:9)



void destroy()

현재 스레드 그룹을 파괴하고 이 그룹과 그 하위 스레드 그룹에 속한 모든 스레드들을 중지시킵니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
group.destroy();
System.out.println("Thread group destroyed.");

출력 결과 예시:Thread group destroyed.



int enumerate(Thread[] list)

현재 스레드 그룹에 속한 활성 스레드들을 지정된 배열에 열거합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");

Thread thread1 = new Thread(group, "Thread1");
Thread thread2 = new Thread(group, "Thread2");

thread1.start();
thread2.start();

Thread[] threads = new Thread[group.activeCount()]; // 배열 크기를 설정합니다.
int count = group.enumerate(threads); // 활성 스레드를 배열에 열거합니다.

System.out.println("Active threads in the group:");
for (int i = 0; i < count; i++) {
    System.out.println(threads[i].getName()); // 각 스레드의 이름을 출력합니다.
}

출력 결과 예시:Active threads in the group:
Thread1
Thread2


int enumerate(Thread[] list, boolean recurse)

현재 스레드 그룹에 속한 활성 스레드들을 지정된 배열에 열거합니다. `recurse` 매개변수가 `true`로 설정되면 하위 스레드 그룹의 스레드들도 재귀적으로 포함됩니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup = new ThreadGroup(parentGroup, "ChildGroup");

Thread thread1 = new Thread(parentGroup, "Thread1");
Thread thread2 = new Thread(childGroup, "Thread2");

thread1.start();
thread2.start();

Thread[] threads = new Thread[parentGroup.activeCount()]; // 배열 크기를 설정합니다.
int count = parentGroup.enumerate(threads, true); // 활성 스레드를 배열에 열거합니다.

System.out.println("Active threads in the parent group and its child groups:");
for (int i = 0; i < count; i++) {
    System.out.println(threads[i].getName()); // 각 스레드의 이름을 출력합니다.
}

출력 결과 예시:Active threads in the parent group and its child groups:
Thread1
Thread2



int enumerate(ThreadGroup[] list)

현재 스레드 그룹에 속한 하위 스레드 그룹들을 지정된 배열에 열거합니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup1 = new ThreadGroup(parentGroup, "ChildGroup1");
ThreadGroup childGroup2 = new ThreadGroup(parentGroup, "ChildGroup2");

ThreadGroup[] groups = new ThreadGroup[parentGroup.activeGroupCount()]; // 배열 크기를 설정합니다.
int count = parentGroup.enumerate(groups); // 하위 스레드 그룹을 배열에 열거합니다.

System.out.println("Subgroups in the parent group:");
for (int i = 0; i < count; i++) {
    System.out.println(groups[i].getName()); // 각 스레드 그룹의 이름을 출력합니다.
}

출력 결과 예시:Subgroups in the parent group:
ChildGroup1
ChildGroup2



int enumerate(ThreadGroup[] list, boolean recurse)

현재 스레드 그룹에 속한 하위 스레드 그룹들을 지정된 배열에 열거합니다. `recurse` 매개변수가 `true`로 설정되면 하위 스레드 그룹들의 하위 스레드 그룹들도 재귀적으로 포함됩니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup1 = new ThreadGroup(parentGroup, "ChildGroup1");
ThreadGroup childGroup2 = new ThreadGroup(parentGroup, "ChildGroup2");

ThreadGroup[] groups = new ThreadGroup[parentGroup.activeGroupCount()]; // 배열 크기를 설정합니다.
int count = parentGroup.enumerate(groups, true); // 하위 스레드 그룹을 배열에 열거합니다.

System.out.println("Subgroups in the parent group and its subgroups:");
for (int i = 0; i < count; i++) {
    System.out.println(groups[i].getName()); // 각 스레드 그룹의 이름을 출력합니다.
}

출력 결과 예시:Subgroups in the parent group and its subgroups:
ParentGroup
ChildGroup1
ChildGroup2



int getMaxPriority()

현재 스레드 그룹의 최대 우선순위를 반환합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
int maxPriority = group.getMaxPriority();
System.out.println("Max priority of the thread group: " + maxPriority);

출력 결과 예시:Max priority of the thread group: 10



String getName()

현재 스레드 그룹의 이름을 반환합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
String groupName = group.getName();
System.out.println("Thread group name: " + groupName);

출력 결과 예시:Thread group name: ExampleGroup



ThreadGroup getParent()

현재 스레드 그룹의 부모 스레드 그룹을 반환합니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup = new ThreadGroup(parentGroup, "ChildGroup");

ThreadGroup parent = childGroup.getParent();
System.out.println("Parent of childGroup: " + parent.getName());

출력 결과 예시:Parent of childGroup: ParentGroup



void interrupt()

현재 스레드 그룹에 속한 모든 스레드들을 인터럽트합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");

Thread thread1 = new Thread(group, () -> {
    while (!Thread.currentThread().isInterrupted()) {
        // 스레드의 작업 수행
    }
});
thread1.start();

Thread thread2 = new Thread(group, () -> {
    while (!Thread.currentThread().isInterrupted()) {
        // 스레드의 작업 수행
    }
});
thread2.start();

group.interrupt(); // 그룹에 속한 모든 스레드를 인터럽트합니다.



boolean isDaemon()
현재 스레드 그룹이 데몬 스레드인지 여부를 반환합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
boolean isDaemon = group.isDaemon();
System.out.println("Is the thread group a daemon? " + isDaemon);

출력 결과 예시:Is the thread group a daemon? false



 

boolean isDestoryed()

현재 스레드 그룹이 파괴되었는지 여부를 반환합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
group.destroy(); // ThreadGroup 파괴

boolean isDestroyed = group.isDestroyed();
System.out.println("Is the thread group destroyed? " + isDestroyed);

출력 결과 예시:Is the thread group destroyed? true

 



void list()

현재 스레드 그룹에 속한 스레드와 하위 스레드 그룹들의 정보를 표시합니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup = new ThreadGroup(parentGroup, "ChildGroup");

Thread thread1 = new Thread(parentGroup, () -> {
    while (true) {
        // 스레드의 작업 수행
    }
}, "Thread1");

thread1.start();

parentGroup.list(); // 현재 스레드 그룹에 속한 스레드와 하위 스레드 그룹들의 정보를 출력합니다.



boolean parentOf(ThreadGroup g)

현재 스레드 그룹이 인자로 전달된 스레드 그룹의 부모인지 여부를 반환합니다.

ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup = new ThreadGroup(parentGroup, "ChildGroup");

boolean isParent = parentGroup.parentOf(childGroup);
System.out.println("Is parentGroup the parent of childGroup? " + isParent);

출력 결과 예시:Is parentGroup the parent of childGroup? true



void setDaemon(boolean daemon)

현재 스레드 그룹을 데몬 스레드 그룹으로 설정하거나 해제합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
group.setDaemon(true); // 스레드 그룹을 데몬 스레드 그룹으로 설정



void setMaxPriority(int pri)

현재 스레드 그룹의 최대 우선순위를 설정합니다.

ThreadGroup group = new ThreadGroup("ExampleGroup");
group.setMaxPriority(Thread.MAX_PRIORITY); // 스레드 그룹의 최대 우선순위를 최대 스레드 우선순위로 설정

 

 

https://standout.tistory.com/1421

 

스레드 구현과 실행 + 싱글쓰레드와 멀티쓰레드: start()와 run()

앞서 스레드에 대해 알아봤다. 프로세스는 공장이라면 스레드는 일꾼이라 생각하면 된다. https://standout.tistory.com/498 프로세스/스레드 방식 프로세스: 공장 스레드: 일꾼 프로세스 방식 매번 새로

standout.tistory.com