Conversation
| try { | ||
| this.store.enter(); | ||
| System.out.println(this.name + " 입장! 현재 손님 수 : " + this.store.getConsumerCount()); | ||
| while (isRun) { |
|
|
||
|
|
||
| public Store() { | ||
| this.maxGoodsCount = new AtomicInteger(10); |
There was a problem hiding this comment.
10 이랑 5는 정해져 있는 숫자이니 상수로 지정해주면 좋을 것 같습니다!
| */ | ||
| public class Consumer implements Runnable { | ||
|
|
||
| Store store; |
| public static void main(String[] args) throws InterruptedException { | ||
| Store store = new Store(); | ||
| Producer producer = new Producer(store); | ||
| ExecutorService executorService = Executors.newFixedThreadPool(5); |
| while (true) { | ||
| System.out.println("물건 공급 중!"); | ||
| int randNum = ThreadLocalRandom.current().nextInt(1, 11); | ||
| Thread.sleep(randNum * 1_000L); |
| * Thread내에서 난수 생성을 위해서는 ThreadLocalRandom.current().nextInt()를 사용하면 된다 | ||
| */ | ||
| public class Producer implements Runnable { | ||
| Thread thread; |
| } | ||
|
|
||
| public synchronized void buy() throws InterruptedException { | ||
| while (true) { |
There was a problem hiding this comment.
while 문에 저 if 문 조건을 넣어주시는 건 어떨까요?
There was a problem hiding this comment.
해당 형태로 구조를 변경하였습니다!
| } | ||
|
|
||
| public synchronized void sell() throws InterruptedException { | ||
| while (true) { |
There was a problem hiding this comment.
이것도 위에 처럼 while 문에 if문 조건을 넣어줘서 하시고 else 문에 있는 친구들은 밖으로 빼줘서 while문이 끝나면 실행이 되도록 하시면 더 명확해 지고 깔끔해 지지 않을 까요?
|
|
||
| public class Consumer implements Runnable { | ||
|
|
||
| Mart mart; |
|
|
||
| public class Consumer implements Runnable { | ||
|
|
||
| Mart mart; |
| public void enter() throws InterruptedException { | ||
|
|
||
| if (this.consumerCount.get() >= this.maxConsumerCount.get()) { | ||
| wait(); |
There was a problem hiding this comment.
여기서 wait을 해준다면 Main.java에서는 따로 조건을 검사하지 않고 스레드 풀이 일정 시간마다 생성만 되도록 구현할 수 있지 않을까욤?
There was a problem hiding this comment.
맞습니다!
main의 조건을 삭제하였습니다!
| } | ||
|
|
||
| public synchronized int getGoodsCount() { | ||
| return this.goodsCount.intValue(); |
There was a problem hiding this comment.
AtomicInteger 타입 안에 get이라는 메서드가 int value를 return해주는데 한번 더 get 메서드를 만드신 이유가 있을까요?
There was a problem hiding this comment.
producer에서 store에 접근하여 물건의 개수를 가져오기 위하여 메서드를 만들어주었습니다!
* mart 구조 수정 * store 세마포어 위치 수정
* store 필요 없는 변수, 메서드 삭제
No description provided.