Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.

Conversation

NANNDA3463
Copy link
Contributor

No description provided.

try {
this.store.enter();
System.out.println(this.name + " 입장! 현재 손님 수 : " + this.store.getConsumerCount());
while (isRun) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 반복문을 쓴 이유가 궁금합니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

삭제했습니다!



public Store() {
this.maxGoodsCount = new AtomicInteger(10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 이랑 5는 정해져 있는 숫자이니 상수로 지정해주면 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가하였습니다!

*/
public class Consumer implements Runnable {

Store store;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

접근제어자가 필요할 것 같습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가했습니다!

public static void main(String[] args) throws InterruptedException {
Store store = new Store();
Producer producer = new Producer(store);
ExecutorService executorService = Executors.newFixedThreadPool(5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쓰레드 풀 쓰셨네요 배워갈게요

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 배워갑니다

while (true) {
System.out.println("물건 공급 중!");
int randNum = ThreadLocalRandom.current().nextInt(1, 11);
Thread.sleep(randNum * 1_000L);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32번째 줄이랑 분리해 놓으신 이유가 있을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메서드로 분리하였습니다!

* Thread내에서 난수 생성을 위해서는 ThreadLocalRandom.current().nextInt()를 사용하면 된다
*/
public class Producer implements Runnable {
Thread thread;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

접근제어자!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가하였습니다!

}

public synchronized void buy() throws InterruptedException {
while (true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while 문에 저 if 문 조건을 넣어주시는 건 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 형태로 구조를 변경하였습니다!

}

public synchronized void sell() throws InterruptedException {
while (true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 위에 처럼 while 문에 if문 조건을 넣어줘서 하시고 else 문에 있는 친구들은 밖으로 빼줘서 while문이 끝나면 실행이 되도록 하시면 더 명확해 지고 깔끔해 지지 않을 까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 형태로 수정하였습니다!


public class Consumer implements Runnable {

Mart mart;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

접근제어자

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해결


public class Consumer implements Runnable {

Mart mart;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

접근제어자!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정!

public void enter() throws InterruptedException {

if (this.consumerCount.get() >= this.maxConsumerCount.get()) {
wait();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 wait을 해준다면 Main.java에서는 따로 조건을 검사하지 않고 스레드 풀이 일정 시간마다 생성만 되도록 구현할 수 있지 않을까욤?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다!
main의 조건을 삭제하였습니다!

}

public synchronized int getGoodsCount() {
return this.goodsCount.intValue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AtomicInteger 타입 안에 get이라는 메서드가 int value를 return해주는데 한번 더 get 메서드를 만드신 이유가 있을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

producer에서 store에 접근하여 물건의 개수를 가져오기 위하여 메서드를 만들어주었습니다!

Copy link
Contributor

@minsu11 minsu11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parkminsu code review

* mart 구조 수정
* store 세마포어 위치 수정
* store 필요 없는 변수, 메서드 삭제
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants