Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/Main.java

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

접근제어자 빠졌습니당

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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
}
33 changes: 33 additions & 0 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Main.java
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}


}
}
}
26 changes: 26 additions & 0 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Mart.java
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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
}
}
}
66 changes: 66 additions & 0 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/practice/Store.java
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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

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

// TODO 는 다 하셨으면 지워주셔도 좋을 것 같습니다!

}
}
System.out.println("물건을 납품했습니다.");
productCount.addAndGet(1);
notifyAll();
}
}
36 changes: 36 additions & 0 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/store/Consumer.java
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;
}
}
27 changes: 27 additions & 0 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/store/Main.java
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) {

}

}

}


}
31 changes: 31 additions & 0 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/store/Producer.java
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;
}
}
61 changes: 61 additions & 0 deletions Thread/src/main/java/com/nhnacademy/jaehyeon/store/Store.java
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

간혹 IndexOutOfBoundsException이 일어나고 있습니다
Index를 활용한 제어는 자제해야 할 것 같습니다
아니면 동시접근 문제 일 수도 있겠네요

Copy link
Contributor

Choose a reason for hiding this comment

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

물건을 판매하는 메소드인데 consumerList에서 remove가 일어나고 있어서 에러가 나는 것 같습니다..!

notifyAll();

}
}