코드 그라데이션

후발대 21일차 전체 코드 본문

Java/후발대

후발대 21일차 전체 코드

완벽한 장면 2023. 3. 3. 23:44

후발대수업_21.

Thread

  • Runnable
  • Join
  • MultiThread
  • Syncronization (동기화)

진행 내용 (수업자료)

실습코드

 

_02_Runnable

package com.sparta.hbd04.prac01.prac20;

import com.sparta.hbd04.prac01.prac20.clean.CleanRunnable;

public class _02_Runnable {
    public static void main(String[] args) {
        CleanRunnable cleanRunnable = new CleanRunnable();
        Thread thread = new Thread(cleanRunnable);
        thread.start();

        cleanByBoss();

    }

    public static void cleanByBoss() {
        System.out.println("--사장 청소 시작--");
        for (int i = 1; i <= 10; i+=2) {
            System.out.println("(사장) " + i +" 번방 청소 중");
        }
        System.out.println("--사장 청소 끝--");
    }
}

clean pkg > CleanRunnable

public class CleanRunnable implements Runnable {
//public class CleanRunnable extends ParentsClass implements Runnable {
		@override
    public void run() {
        System.out.println("--직원 청소 시작 (Runnable)--");
        for (int i = 2; i <= 10; i+=2) {
            System.out.println("(직원) " + i +" 번방 청소 중 (Runnable)");
        }
        System.out.println("--직원 청소 끝 (Runnable)--");
    }
}

_03_Join

package com.sparta.hbd04.prac01.prac20;

import com.sparta.hbd04.prac01.prac20.clean.CleanRunnable;

public class _03_Join {
    public static void main(String[] args) {
        CleanRunnable cleanRunnable = new CleanRunnable();
        Thread thread = new Thread(cleanRunnable);
        thread.start();

        try {
//            thread.join();
            thread.join(2500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        cleanByBoss();
    }
    public static void cleanByBoss() {
        System.out.println("--사장 청소 시작--");
        for (int i = 1; i <= 10; i+=2) {
            System.out.println("(사장) " + i +" 번방 청소 중");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("--사장 청소 끝--");
    }
}

_04_MultiThread

package com.sparta.hbd04.prac01.prac20;

public class _04_MultiThread {
    public static void main(String[] args) {

        Runnable cleaner1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("--직원1 청소 시작 --");
                for (int i = 1; i <= 10; i+=2) {
                    System.out.println("(직원1) " + i +" 번방 청소 중");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("--직원1 청소 끝 --");
            }
        };

        Runnable cleaner2 =() -> {
            System.out.println("--직원2 청소 시작 --");
            for (int i = 2; i <= 10; i+=2) {
                System.out.println("(직원2) " + i +" 번방 청소 중 ");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("--직원2 청소 끝 --");
        };

        Thread cleaner1Thread = new Thread(cleaner1);
        Thread cleaner2Thread = new Thread(cleaner2);

        cleaner1Thread.start();
        cleaner2Thread.start();

    }
}

_05_Syncronization

package com.sparta.hbd04.prac01.prac20;

import com.sparta.hbd04.prac01.prac20.clean.Room;

public class _05_Syncronizaton {
    public static void main(String[] args) {
        Room room = new Room();

        Runnable cleaner1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("--직원1 청소 시작 --");
                for (int i = 1; i <= 5; i++) {
                    room.clean("직원1");

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    if(i==2) {
                        throw new RuntimeException("못해 먹겠다~~");
                    }

                }
                System.out.println("--직원1 청소 끝 --");
            }
        };

        Runnable cleaner2 =() -> {
            System.out.println("--직원2 청소 시작 --");
            for (int i = 1; i <= 5; i++) {
               room.clean("직원2");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
            System.out.println("--직원2 청소 끝 --");
        };

        Thread cleaner1Thread = new Thread(cleaner1);
        Thread cleaner2Thread = new Thread(cleaner2);

        cleaner1Thread.start();
        cleaner2Thread.start();

    }
}

clean pkg > Room

package com.sparta.hbd04.prac01.prac20.clean;

public class Room {
    public int number =1;
    synchronized public void clean(String name) {
        // 직원 1 : 3번방 청소중
        System.out.println(name + ": " + number +"번방 청소중" );
        number++;
    }
}

스레드의 개념


프로세스(process)란?

프로세스(process)란 단순히 실행 중인 프로그램(program)이라고 할 수 있습니다.

즉, 사용자가 작성한 프로그램이 운영체제에 의해 메모리 공간을 할당받아 실행 중인 것을 말합니다.

이러한 프로세스는 프로그램에 사용되는 데이터와 메모리 등의 자원 그리고 스레드로 구성됩니다.


스레드(thread)란?

스레드(thread)란 프로세스(process) 내에서 실제로 작업을 수행하는 주체를 의미합니다.

모든 프로세스에는 한 개 이상의 스레드가 존재하여 작업을 수행합니다.

또한, 두 개 이상의 스레드를 가지는 프로세스를 멀티스레드 프로세스(multi-threaded process)라고 합니다.


스레드의 생성과 실행

자바에서 스레드를 생성하는 방법에는 다음과 같이 두 가지 방법이 있습니다.

  1. Runnable 인터페이스를 구현하는 방법
  2. Thread 클래스를 상속받는 방법

그리고

  1. Join 활용
  2. 멀티스레드
  3. 동기화

를 실습해 보는 시간을 갖는다.

728x90

'Java > 후발대' 카테고리의 다른 글

후발대 22일차(1) 전체 코드  (0) 2023.03.07
후발대 21일차 설명 추가  (0) 2023.03.04
예외처리 Quiz  (0) 2023.03.03
후발대 20일차 설명 추가  (0) 2023.03.02
후발대 20일차 전체 코드  (0) 2023.03.02
Comments