Java adv1 - 생산자 , 소비자 문제

dev1·2024년 12월 2일

전체 코드에서 결국,

한정된 크기의 리스트를 가지니까 ....

크기를 넘게되면 데이터를 추가로 넣을 수 없는 상황 ==> 소비하는 입장에서도 가져갈 수 없는상황

package thread.bounded;

import java.util.ArrayList;
import java.util.List;

import static util.MyLogger.log;
import static util.ThreadUtils.sleep;

public class boundedMain {

    public static void main(String[] args) {
        // queue 선택
        BoundedQueueV1 queue = new BoundedQueueV1(2);

        // 생산자, 소비자 실행 순서 선택 /// 반드시 한개만
        producerFirst(queue);
        //consumerFirst(queue);
    }

    private static void consumerFirst(BoundedQueue queue) {
        log("== [소비자 먼저 실행] 시작, " + queue.getClass().getSimpleName() + "==");
        List<Thread> threads = new ArrayList<>();
        startConsumer(queue, threads);
        printAllState(queue, threads);
        startProducer(queue, threads);
        printAllState(queue, threads);
        log("== [소비자 먼저 실행] 종료, " + queue.getClass().getSimpleName() + "==");
    }

    private static void producerFirst(BoundedQueueV1 queue) {
        log("== [생산자 먼저 실행] 시작, " + queue.getClass().getSimpleName() + "==");
        List<Thread> threads = new ArrayList<>();
        startProducer(queue, threads);
        printAllState(queue, threads);
        startConsumer(queue, threads);
        printAllState(queue, threads);
        log("== [생산자 먼저 실행] 종료, " + queue.getClass().getSimpleName() + "==");

    }

    private static void startConsumer(BoundedQueue queue, List<Thread> threads) {
        System.out.println();
        log("소비자 시작");
        for (int i = 1; i <= 3; i++) {
            Thread consumer = new Thread(new ConsumerTask(queue), "consumer" + i);
            threads.add(consumer);
            consumer.start();
            sleep(100);
        }
    }

    private static void printAllState(BoundedQueue queue, List<Thread> threads) {
        System.out.println();
        log("현재 상태 출려그 큐 데이터 : " + queue);
        for (Thread thread : threads) {
            log(thread.getName() + " 상태 : " + thread.getState());
        }
    }

    private static void startProducer(BoundedQueue queue, List<Thread> threads) {
        System.out.println();
        log("생산자 시작");
        for (int i = 1; i <= 3; i++) {
            Thread producer = new Thread(new ProducerTask(queue, "data" + i), "producer" + i );
            threads.add(producer);
            producer.start();
            sleep(100);
        }
    }


}

지금은 크기가2 ... /// 2개까지 넣는거는 ㅇㅋ

근데? 3개를 넣으려고하는순간,

생산부터 우선적으로 한다고 가정하면, 이미 크기가 꽉 차있기때문에 추가로 데이터를 없음

즉 3번째 넣으려고 하는 데이터는 못넣고, 이 데이터가 없어지는셈인거

그럼, 소비하는 입장에서도

1, 2 번째 오는 소비자는 ㅇㅋ 데이터 가져갈 수 있음

근데? 3번째 오는애는 데이터를 가져갈 수 없음

왜? 배열에 데이터가 2개밖에없는데 앞에있는 2명이 다 가져갔고,

3번째 데이터는 존재하지 않으니가 ( 버려짐 ).

그럼 이러한 문제를 어떻게 해결 ?

생산자 우선인것을 생각해보면 .... 배열이 꽉 차 있다고 하더라도, 기다렸다가 배열이 비었을때, 데이터 추가

즉, 3번째 데이터가 데이터를 넣으려고할때, 배열이 꽉 차 있다고하면 ....

바로 데이터를 그냥 버리는게아니라, 계속해서 배열의 사이즈를 체크

그러다가, 누군가가 소비를해서 배열 데이터가 줄어드는 상황이 있을거고,

이때 데이터를 뿅 하고 넣어주면됨 그렇게되면, 버려지는 데이터가 없을거고 소비하는 입장에서도 가져갈 수 있음

그럼 이 배열을 확인하는 것을 어떻게 ???

===> while 문으로 계속해서 체크 ....

0개의 댓글