-
Notifications
You must be signed in to change notification settings - Fork 0
thread upload #43
base: main
Are you sure you want to change the base?
thread upload #43
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.example.thread.assignment01; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class Consumer implements Runnable { | ||
private Store store; | ||
private String name; | ||
private Random random; | ||
public static int count = 0; | ||
|
||
public Consumer(String name, Store store) { | ||
this.store = store; | ||
this.name = name + ++count; | ||
|
||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
store.enter(this); | ||
sellRandomTime(); | ||
store.exit(this); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
|
||
private void sellRandomTime() { | ||
try { | ||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000)); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
System.out.println("Thread Interrupted"); | ||
} | ||
pickFoods(); | ||
} | ||
|
||
public void pickFoods() { | ||
int productNum = randomProductNum(); | ||
int amount = randomAmount(); | ||
Product product = store.getFoodStand().getFoodsList().get(productNum); | ||
store.getFoodStand().sell(product, amount); | ||
} | ||
|
||
private int randomAmount() { | ||
random = new Random(); | ||
return random.nextInt(10) + 1; | ||
} | ||
|
||
private int randomProductNum() { | ||
random = new Random(); | ||
return random.nextInt(store.getFoodStand().getFoodsList().size()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.example.thread.assignment01; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class FoodStand { | ||
private List<Product> productList; | ||
|
||
public FoodStand() { | ||
this.productList = new ArrayList<>(); | ||
} | ||
|
||
|
||
public List<Product> getFoodsList() { | ||
return this.productList; | ||
} | ||
|
||
public void add(Product product, int amount) { | ||
if(productList.contains(product)) { | ||
product.add(amount); | ||
} else { | ||
productList.add(product); | ||
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. 물건 리스트에 추가하고, 그 물건의 amount도 add해주는 식이면 더 의도에 맞을 수도 있겠습니다 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. product클래스 내부의 add메서드에서 구현하고 있습니다 |
||
} | ||
} | ||
|
||
public void sell(Product product, int amount) { //상품 판매 | ||
if(productList.contains(product)) { | ||
product.sell(amount); | ||
} else { | ||
throw new IllegalArgumentException("해당 상품이 존재하지 않습니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.example.thread.assignment01; | ||
|
||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws InterruptedException { | ||
|
||
Store store = new Store(); | ||
Producer producer = new Producer(store); | ||
Thread producerThread = new Thread(producer); | ||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); | ||
Runnable consumerTask = () -> { | ||
Consumer consumer = new Consumer("Consumer", store); | ||
Thread consumerThread = new Thread(consumer); | ||
consumerThread.start(); | ||
}; | ||
executorService.scheduleAtFixedRate(consumerTask, 0, 5, TimeUnit.SECONDS); | ||
producerThread.start(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.example.thread.assignment01; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import org.example.thread.assignment01.product.*; | ||
|
||
public class Producer implements Runnable { | ||
private Store store; | ||
private List<Product> itemList; | ||
private Random random; | ||
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. 생성자에서 초기화 해줘도 좋을 것 같습니다 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. 수정했습니다! |
||
|
||
public Producer(Store store) { | ||
this.store = store; | ||
this.itemList = new ArrayList<>(); | ||
initialSetting(); | ||
} | ||
|
||
private void initialSetting() { | ||
itemList.add(new Vegetable("야채", 30)); | ||
itemList.add(new ProcessedFood("조리 식품", 30)); | ||
itemList.add(new DriedSeafood("건어물", 30)); | ||
itemList.add(new CannedFoods("기호 식품", 30)); | ||
itemList.add(new OtherItems("기타", 30)); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (!Thread.interrupted()) { | ||
try { | ||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
System.out.println("Thread Interrupted"); | ||
} | ||
|
||
randomDelivery(); | ||
} | ||
} | ||
|
||
private void randomDelivery() { | ||
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. randomDelivery가 배달하는 행동이라고 생각을 하는데, 메서드 안에서 store.getFoodStand를 호출해서 add를 하는 방식 대신에 product를 return을 시켜주는 방식은 어떨까요?? 그리고 store에서 return 값을 받는 메서드를 만든 다음에 Producer run메서드에 store."메서드 명"(randomDelivery()) 이런 방식으로 해주는건 어떨까여? 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. 오! 참고해서 수정했습니다. |
||
int deliveryNum = randomDeliveryNum(); | ||
int amount = randomAmount(); | ||
Product product = bringProduct(deliveryNum); | ||
store.getFoodStand().add(product, amount); | ||
|
||
} | ||
private int randomAmount() { | ||
random = new Random(); | ||
return random.nextInt(10) + 1; | ||
} | ||
|
||
private int randomDeliveryNum() { | ||
random = new Random(); | ||
return random.nextInt(itemList.size()); | ||
} | ||
|
||
private Product bringProduct(int deliveryNum) { | ||
return itemList.get(deliveryNum); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.example.thread.assignment01; | ||
|
||
public class Product { | ||
|
||
private String name; | ||
private int amount; | ||
public static final int MAX_NUM = 30; | ||
public Product(String name, int amount) { | ||
this.name = name; | ||
this.amount = amount; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public synchronized boolean isAbleBuy(int amount) { | ||
|
||
if (amount > 30) { | ||
System.out.println(this.name + "의 재고는 30개까지만 가능합니다."); | ||
return false; | ||
} | ||
if (this.amount + amount > MAX_NUM) { | ||
System.out.println(this.name + "의 재고는 30개까지만 가능합니다."); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public synchronized boolean isAbleSell(int amount) { | ||
return (this.amount - amount >= 0); | ||
} | ||
|
||
public synchronized void add(int amount) { | ||
while (!isAbleBuy(amount)) { | ||
try { | ||
wait(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
this.amount += amount; | ||
System.out.printf("%s %d개가 가판대에 추가 되었습니다.\n", this.name, amount); | ||
notifyAll(); | ||
} | ||
|
||
public synchronized void sell(int amount) { //상품 판매 | ||
while (!isAbleSell(amount)) { | ||
try { | ||
wait(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
if(isAbleSell(amount)) { | ||
this.amount -= amount; | ||
System.out.printf("%s %d개 구매 완료.\n", this.name, amount); | ||
System.out.printf("남은 %s의 수량: %d\n", this.name, this.amount); | ||
notifyAll(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.example.thread.assignment01; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Store { | ||
private List<Consumer> people; | ||
|
||
private FoodStand foodStand; | ||
|
||
public static final int PEOPLE_NUM_LIMIT = 5; | ||
|
||
|
||
public Store() { | ||
people = new ArrayList<>(); | ||
foodStand = new FoodStand(); | ||
} | ||
|
||
public FoodStand getFoodStand() { | ||
return foodStand; | ||
} | ||
|
||
public synchronized boolean isAbleEnter() { | ||
return people.size() < PEOPLE_NUM_LIMIT; | ||
} | ||
|
||
public synchronized void enter(Consumer consumer) { | ||
if (consumer == null) { | ||
throw new IllegalArgumentException("consumer is null"); | ||
} | ||
while (!isAbleEnter()) { | ||
try { | ||
System.out.println("대기중입니다 ..."); | ||
wait(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
people.add(consumer); | ||
System.out.println(consumer.getName() + " enter"); | ||
} | ||
|
||
public synchronized void exit(Consumer consumer) { | ||
if (people.contains(consumer)) { | ||
|
||
people.remove(consumer); | ||
System.out.println(consumer.getName() + " exit"); | ||
notifyAll(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.example.thread.assignment01.product; | ||
|
||
import org.example.thread.assignment01.Product; | ||
|
||
public class CannedFoods extends Product { | ||
|
||
public CannedFoods(String name, int amount) { | ||
super(name, amount); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.example.thread.assignment01.product; | ||
|
||
import org.example.thread.assignment01.Product; | ||
|
||
public class DriedSeafood extends Product { | ||
public DriedSeafood(String name, int amount) { | ||
super(name, amount); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.example.thread.assignment01.product; | ||
|
||
import org.example.thread.assignment01.Product; | ||
|
||
public class OtherItems extends Product { | ||
public OtherItems(String name, int amount) { | ||
super(name, amount); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.example.thread.assignment01.product; | ||
|
||
import org.example.thread.assignment01.Product; | ||
|
||
public class ProcessedFood extends Product { | ||
public ProcessedFood(String name, int amount) { | ||
super(name, amount); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.example.thread.assignment01.product; | ||
|
||
import org.example.thread.assignment01.Product; | ||
|
||
public class Vegetable extends Product { | ||
public Vegetable(String name, int amount) { | ||
super(name, amount); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
static int로 하신 이유가 있을까여?? 그리고 접근 제어자를 public으로 하신 이유가 있을까여?
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.
Consumer 인스턴스가 몇개 생성하는지 count 하고 싶으셔서 하는게 아닐까요?? 접근제어자는 이 변수가 이 클래스에서만 쓰인다면 private로 해주시는게 좋지 않을까요?
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.
consumer가 새로 생성되더라도 count수는 유지하기 위해서 static 사용했습니다.
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.
접근제어자 private로 수정했습니다.