상세 컨텐츠

본문 제목

[스레드 풀 종료]

자바

by esoesmio 2023. 4. 4. 21:32

본문

1부터 100까지 도는 스레드풀을 종료시켜라

package april4;

import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class practice2 {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(5);
es.execute(new thread1());
es.execute(new thread2());



        try {

            if(!es.awaitTermination(5, TimeUnit.SECONDS))
            {es.shutdownNow();}
        } catch (InterruptedException e) {
Thread.currentThread().interrupt();        }


    }

}

class thread1 extends Thread{
    @Override
    public void run(){

        for(int i=0;i<100;i++){

            System.out.println("쓰레드1 현재 " + i);

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                System.out.println("셧다운 호출");
                break;
            }


        }

    }



}

class thread2 extends Thread{
    @Override
    public void run(){

        for(int i=0;i<100;i++){

            System.out.println("쓰레드2 현재 " + i);

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                System.out.println("셧다운 호출");
                break;
            }


        }

    }



}

관련글 더보기

댓글 영역