|
| 1 | +package thread.bounded; |
| 2 | + |
| 3 | +import util.MyLogger; |
| 4 | +import util.ThreadUtils; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +public class BoundedMain { |
| 10 | + public static void main(String[] args) { |
| 11 | + |
| 12 | + // 1. BoundedQueue 선택 |
| 13 | + BoundedQueue queue = new BoundedQueueV1(2); |
| 14 | + |
| 15 | + // 2. 생산자, 소비자 실행 순서 선택, 반드 하나만 선탹! |
| 16 | +// producerFirst(queue); |
| 17 | + consumerFirst(queue); |
| 18 | + } |
| 19 | + |
| 20 | + private static void producerFirst(BoundedQueue queue){ |
| 21 | + MyLogger.log("== [생산자 먼저 실행] 시작, " + queue.getClass().getSimpleName() + " == "); |
| 22 | + List<Thread> threads = new ArrayList<>(); |
| 23 | + startProducer(queue, threads); |
| 24 | + printAllState(queue, threads); |
| 25 | + startConsumer(queue, threads); |
| 26 | + printAllState(queue, threads); |
| 27 | + MyLogger.log("== [생산자 먼저 실행] 종료, " + queue.getClass().getSimpleName() + " == "); |
| 28 | + } |
| 29 | + |
| 30 | + private static void consumerFirst(BoundedQueue queue){ |
| 31 | + MyLogger.log("== [소비자 먼저 실행] 시작, " + queue.getClass().getSimpleName() + " == "); |
| 32 | + List<Thread> threads = new ArrayList<>(); |
| 33 | + startConsumer(queue, threads); |
| 34 | + printAllState(queue, threads); |
| 35 | + startProducer(queue, threads); |
| 36 | + printAllState(queue, threads); |
| 37 | + MyLogger.log("== [소비자 먼저 실행] 종료, " + queue.getClass().getSimpleName() + " == "); |
| 38 | + } |
| 39 | + |
| 40 | + private static void startConsumer(BoundedQueue queue, List<Thread> threads) { |
| 41 | + System.out.println(); |
| 42 | + MyLogger.log("소비자 시작"); |
| 43 | + for (int i = 1; i <= 3; i++) { |
| 44 | + Thread consumer = new Thread(new ConsumerTask(queue), "consumer" + i); |
| 45 | + threads.add(consumer); |
| 46 | + consumer.start(); |
| 47 | + ThreadUtils.sleep(100); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static void startProducer(BoundedQueue queue, List<Thread> threads){ |
| 52 | + System.out.println(); |
| 53 | + MyLogger.log("생성자 시작"); |
| 54 | + for (int i = 1; i <= 3; i++) { |
| 55 | + Thread producer = new Thread(new ProducerTask(queue, "data" + i), "proudcer" + i); |
| 56 | + threads.add(producer); |
| 57 | + producer.start(); |
| 58 | + ThreadUtils.sleep(100); |
| 59 | + } |
| 60 | + } |
| 61 | + private static void printAllState(BoundedQueue queue, List<Thread> threads) { |
| 62 | + System.out.println(); |
| 63 | + MyLogger.log("현제 상태 출력, 큐 데이터 : " + queue); |
| 64 | + for(Thread thread : threads) { |
| 65 | + MyLogger.log(thread.getName() + ": " + thread.getState()); |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | +} |
0 commit comments