-
Notifications
You must be signed in to change notification settings - Fork 0
Jaehyeon's Thread Code Review #40
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getStore에서 null값을 받을 수 있는데 이 상황에 대한 예외처리도 필요한 것 같습니다. |
||
| 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(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 캐치를 하고 아무것도 안할 경우 try{
~~~
} catch(Exception ignore)같은 식으로 변수명을 ignore로 명시해주곤 한다고 합니다 |
||
|
|
||
| } | ||
|
|
||
| for (int i = 0; i < storeNumber; i++) { | ||
| count++; | ||
| Thread consumerThread = new Thread(new Consumer("Consumer " + count, mart, i)); | ||
| consumerThread.start(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.nhnacademy.jaehyeon.practice; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Mart { | ||
| List<Store> 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; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문제에서
라는 항목이 있는데, 이러면 마트가 어떤 품목(store)인지 item을 통해 알 수 있는 것 같습니다 |
||
| try { | ||
| Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 1500)); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semaphore를 입장할 때 걸어주면 되겠네요! |
||
| } catch (InterruptedException e) { | ||
| //TODO | ||
| } | ||
|
|
||
| this.consumerCount.addAndGet(1); | ||
| System.out.println("손님 입장"); | ||
|
|
||
| } | ||
|
|
||
| public void exit() { | ||
| this.consumerCount.addAndGet(-1); | ||
| System.out.println("손님 퇴장"); | ||
|
|
||
| semaphore.release(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semahpore 을 이런 식으로 사용하는 거군요! 배워갑니다! |
||
| } | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // TODO 는 다 하셨으면 지워주셔도 좋을 것 같습니다! |
||
| } | ||
| } | ||
| System.out.println("물건을 납품했습니다."); | ||
| productCount.addAndGet(1); | ||
| notifyAll(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.nhnacademy.jaehyeon.store; | ||
|
|
||
| public class Product { | ||
| int number; | ||
|
|
||
| public Product(int number) { | ||
| this.number = number; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.nhnacademy.jaehyeon.store; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Store { | ||
| volatile List<Product> productList; | ||
| List<Consumer> 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 간혹 IndexOutOfBoundsException이 일어나고 있습니다
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 물건을 판매하는 메소드인데 consumerList에서 remove가 일어나고 있어서 에러가 나는 것 같습니다..! |
||
| notifyAll(); | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
접근제어자 빠졌습니당