From 14bd168b9693ddc0ed0cdb8662f1735281cf5da3 Mon Sep 17 00:00:00 2001 From: hyeonjaez Date: Mon, 6 Nov 2023 10:23:45 +0900 Subject: [PATCH] FEAT : Thread study --- .../java/com/nhnacademy/jaehyeon/Main.java | 4 -- .../jaehyeon/practice/Consumer.java | 29 ++++++++ .../nhnacademy/jaehyeon/practice/Main.java | 33 ++++++++++ .../nhnacademy/jaehyeon/practice/Mart.java | 26 ++++++++ .../jaehyeon/practice/Producer.java | 25 +++++++ .../nhnacademy/jaehyeon/practice/Store.java | 66 +++++++++++++++++++ .../nhnacademy/jaehyeon/store/Consumer.java | 36 ++++++++++ .../com/nhnacademy/jaehyeon/store/Main.java | 27 ++++++++ .../nhnacademy/jaehyeon/store/Producer.java | 31 +++++++++ .../nhnacademy/jaehyeon/store/Product.java | 9 +++ .../com/nhnacademy/jaehyeon/store/Store.java | 61 +++++++++++++++++ 11 files changed, 343 insertions(+), 4 deletions(-) delete mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/Main.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Consumer.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Main.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Mart.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Producer.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Store.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/store/Consumer.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/store/Main.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/store/Producer.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/store/Product.java create mode 100644 Thread/src/main/java/com/nhnacademy/jaehyeon/store/Store.java diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/Main.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/Main.java deleted file mode 100644 index c1756c4..0000000 --- a/Thread/src/main/java/com/nhnacademy/jaehyeon/Main.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.nhnacademy.jaehyeon; - -public class Main { -} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Consumer.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Consumer.java new file mode 100644 index 0000000..46762fa --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Consumer.java @@ -0,0 +1,29 @@ +package com.nhnacademy.jaehyeon.practice; + +import java.util.concurrent.ThreadLocalRandom; + +public class Consumer implements Runnable { + + Mart mart; + String name; + int item; + + public Consumer(String name, Mart mart, int item) { + this.name = name; + this.mart = mart; + this.item = item; + } + + @Override + public void run() { + this.mart.getStore(this.item).enter(); + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000)); + this.mart.getStore(this.item).buy(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + this.mart.getStore(this.item).exit(); + } +} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Main.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Main.java new file mode 100644 index 0000000..683bd0a --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Main.java @@ -0,0 +1,33 @@ +package com.nhnacademy.jaehyeon.practice; + +import java.util.concurrent.ThreadLocalRandom; + +public class Main { + public static void main(String[] args) { + + int storeNumber = 5; + Mart mart = new Mart(storeNumber); + + for (int i = 0; i < storeNumber; i++) { + Thread producerThread = new Thread(new Producer(mart, i)); + producerThread.start(); + } + + int count = 0; + while (true) { + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 1500)); + } catch (InterruptedException e) { + + } + + for (int i = 0; i < storeNumber; i++) { + count++; + Thread consumerThread = new Thread(new Consumer("Consumer " + count, mart, i)); + consumerThread.start(); + } + + + } + } +} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Mart.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Mart.java new file mode 100644 index 0000000..644b28f --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Mart.java @@ -0,0 +1,26 @@ +package com.nhnacademy.jaehyeon.practice; + +import java.util.ArrayList; +import java.util.List; + +public class Mart { + List storeList; + int storeNumber; + + public Mart(int storeNumber) { + this.storeList = new ArrayList<>(); + this.storeNumber = storeNumber; + + for (int i = 0; i < storeNumber; i++) { + this.storeList.add(new Store()); + } + } + + public Store getStore(int item) { + if (item >= 0 && item < storeNumber) { + return this.storeList.get(item); + } else { + return null; + } + } +} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Producer.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Producer.java new file mode 100644 index 0000000..30665df --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Producer.java @@ -0,0 +1,25 @@ +package com.nhnacademy.jaehyeon.practice; + +import java.util.concurrent.ThreadLocalRandom; + +public class Producer implements Runnable { + Mart mart; + int item; + + public Producer(Mart mart, int item) { + this.mart = mart; + this.item = item; + } + + @Override + public void run() { + while (true) { + this.mart.getStore(item).sell(); + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 1500)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Store.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Store.java new file mode 100644 index 0000000..d2912d5 --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Store.java @@ -0,0 +1,66 @@ +package com.nhnacademy.jaehyeon.practice; + +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; + +public class Store { + + private static final int MAX_PRODUCT_COUNT = 10; + private static final int MAX_PERSON_COUNT = 5; + + AtomicInteger consumerCount; + AtomicInteger productCount; + + Semaphore semaphore; + + public Store() { + this.consumerCount = new AtomicInteger(0); + this.productCount = new AtomicInteger(0); + semaphore = new Semaphore(MAX_PERSON_COUNT); + } + + public void enter() { + try { + semaphore.acquire(); + } catch (InterruptedException e) { + //TODO + } + + this.consumerCount.addAndGet(1); + System.out.println("손님 입장"); + + } + + public void exit() { + this.consumerCount.addAndGet(-1); + System.out.println("손님 퇴장"); + + semaphore.release(); + } + + public synchronized void buy() { + while (productCount.get() == 0) { + try { + wait(); + } catch (InterruptedException e) { + //TODO + } + } + System.out.println("손님이 물건을 샀습니다."); + productCount.addAndGet(-1); + notifyAll(); + } + + public synchronized void sell() { + while (productCount.get() >= MAX_PRODUCT_COUNT) { + try { + wait(); + } catch (InterruptedException e) { + //TODO + } + } + System.out.println("물건을 납품했습니다."); + productCount.addAndGet(1); + notifyAll(); + } +} \ No newline at end of file diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Consumer.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Consumer.java new file mode 100644 index 0000000..2c855c6 --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Consumer.java @@ -0,0 +1,36 @@ +package com.nhnacademy.jaehyeon.store; + +import java.util.concurrent.ThreadLocalRandom; + +public class Consumer implements Runnable { + Store store; + String name; + static int count = 0; + + public Consumer(Store store) { + this.store = store; + this.name = "Consumer" + count++; + } + + + + + @Override + public void run() { + try { + store.enter(this); + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); + store.sell(); + store.exit(this); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + + } + + @Override + public String toString() { + return name; + } +} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Main.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Main.java new file mode 100644 index 0000000..373ebcb --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Main.java @@ -0,0 +1,27 @@ +package com.nhnacademy.jaehyeon.store; + +import java.util.concurrent.ThreadLocalRandom; + +public class Main { + public static void main(String[] args) { + Store store = new Store(); + Thread consumer = new Thread(new Consumer(store)); + Thread producer = new Thread(new Producer(store)); + + consumer.start(); + producer.start(); + while (true) { + try { + Thread thread = new Thread(new Consumer(store)); + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); + thread.start(); + } catch (InterruptedException e) { + + } + + } + + } + + +} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Producer.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Producer.java new file mode 100644 index 0000000..6400f49 --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Producer.java @@ -0,0 +1,31 @@ +package com.nhnacademy.jaehyeon.store; + +import java.util.concurrent.ThreadLocalRandom; + +public class Producer implements Runnable { + int count; + Store store; + + public Producer(Store store) { + this.store = store; + count = 0; + } + + public Product makeProduct() { + count++; + return new Product(count); + } + + @Override + public void run() { + while(true){ + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 2000)); + store.buy(makeProduct()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + } +} \ No newline at end of file diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Product.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Product.java new file mode 100644 index 0000000..c4a958d --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Product.java @@ -0,0 +1,9 @@ +package com.nhnacademy.jaehyeon.store; + +public class Product { + int number; + + public Product(int number) { + this.number = number; + } +} diff --git a/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Store.java b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Store.java new file mode 100644 index 0000000..71a4c44 --- /dev/null +++ b/Thread/src/main/java/com/nhnacademy/jaehyeon/store/Store.java @@ -0,0 +1,61 @@ +package com.nhnacademy.jaehyeon.store; + +import java.util.ArrayList; +import java.util.List; + +public class Store { + volatile List productList; + List consumerList; + + private static final int MAX_PRODUCT_COUNT = 10; + private static final int MAX_CONSUMER_COUNT = 5; + + public Store() { + this.productList = new ArrayList<>(); + this.consumerList = new ArrayList<>(); + } + + public synchronized void enter(Consumer consumer) { + while (this.consumerList.size() >= MAX_CONSUMER_COUNT) { + + System.out.println("꽉찼습니다 기다려주세요"); + + } + this.consumerList.add(consumer); + System.out.println(consumer + "이 들어왔습니다."); + } + + public synchronized void exit(Consumer consumer) { + System.out.println(consumer + "이 나갑니다"); + this.consumerList.remove(consumer); + notifyAll(); + + } + + public synchronized void buy(Product product) { + while (this.productList.size() >= MAX_PRODUCT_COUNT) { + try { + wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + System.out.println("물건 샀습니다."); + this.productList.add(product); + notifyAll(); + } + + public synchronized void sell() { + while (this.productList.isEmpty()) { + try { + wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + System.out.println("물건 팔았습니다"); + this.consumerList.remove(0); + notifyAll(); + + } +} \ No newline at end of file