상세 컨텐츠

본문 제목

[Thread] ThreadGroup : 그룹 지정

Java

by kwanghyup 2020. 6. 16. 00:06

본문

SamsungSDS.java

public class SamsungSDS implements Runnable{
    
    @Override
    public void run() {
        /*삼성SDS*/
    }
}

 

SamsungDisplay.java

public class SamsungDisplay implements Runnable{

    @Override
    public void run() {
        /*삼성디스플레이*/
    }
}

 

LGUplug.java

public class LGUplus implements Runnable {
    
    @Override
    public void run() {
        /*엘지 유플러스*/
    }
}

 

LGelectronics.java

public class LGelectronics implements Runnable{
    @Override
    public void run() {
        /*엘지 전자*/
    }
}

 

ThreadGroupMain.java

import java.util.Arrays;
import java.util.List;

public class ThreadGroupMain {

    public static void main(String[] args) throws InterruptedException {

        // ThreadGroup 생성
        ThreadGroup main = Thread.currentThread().getThreadGroup();
        ThreadGroup samsung = new ThreadGroup("삼성그룹");
        ThreadGroup lg = new ThreadGroup("엘지그룹");

        // Runnable 객체 구현
        Runnable samsung1 = new SamsungSDS();
        Runnable samsung2 = new SamsungDisplay();
        Runnable lg1 = new LGelectronics();
        Runnable lg2 = new LGUplus();

        // 쓰레드 생성,이름,그룹 지정
        Thread samsungSDS = new Thread(samsung,"삼성SDS");
        Thread samsungDisplay = new Thread(samsung,"삼성디스플레이");
        Thread lgElectronics = new Thread(lg,"엘지전자");
        Thread lgUplus = new Thread(lg,"엘지유플러스");

        // 출력
        List<Thread> threadList = Arrays.asList(samsungSDS,samsungDisplay,lgElectronics,lgUplus);

        for(Thread thread : threadList){
            System.out.println("쓰레드 이름 : "+thread.getName());
            System.out.println("쓰레드 그룹 : "+thread.getThreadGroup().getName());
            System.out.println("이 쓰레드 그룹의 부모 그룹 : " + thread.getThreadGroup().getParent().getName());
            System.out.println("================================================");
        }
    }
}

 

관련글 더보기

댓글 영역