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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.nhnacademy.parkminsu.assignment;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean;

public class Consumer implements Runnable {
private Mart mart;
private Thread thread;
private AtomicBoolean isCheck;

private int buyItem;

public Consumer(Mart mart, String name) {
this.mart = mart;
this.thread = new Thread(this, name);
this.isCheck = new AtomicBoolean(false);
}

public void start() {
thread.start();
}

public void stop() {
thread.interrupt();
}

public void join() throws InterruptedException {
Copy link
Contributor

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.

추후에 사용할 수 있기 때문에 join을 만들었습니다!

thread.join();
}

public Thread getThread() {
return thread;
}


@Override
public void run() {
getBuyItem();
this.mart.enterStore(buyItem);
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000));
this.mart.sellStoreItem(buyItem);
System.out.println(thread.getName() + "가 구매했습니다");
isCheck.set(true);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println(e.getMessage());

}
this.mart.exitStore(buyItem);
}

private void getBuyItem() {
Random random = new Random();
this.buyItem = random.nextInt(this.mart.getStore().length);
}
}
29 changes: 29 additions & 0 deletions Thread/src/main/java/com/nhnacademy/parkminsu/assignment/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.nhnacademy.parkminsu.assignment;


import com.nhnacademy.parkminsu.assignment.exception.NegativeNumException;

import java.util.concurrent.ThreadLocalRandom;


public class Main {
public static void main(String[] args) {
try {
Mart mart = new Mart(5);
String name = "손님";
int loop = 1;

Producer producer = new Producer(mart);
producer.start();

while (true) {
Consumer consumer = new Consumer(mart, name + loop);
consumer.start();
loop += 1;
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000));
}
} catch (NegativeNumException | InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
55 changes: 55 additions & 0 deletions Thread/src/main/java/com/nhnacademy/parkminsu/assignment/Mart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.nhnacademy.parkminsu.assignment;


import com.nhnacademy.parkminsu.assignment.exception.NegativeNumException;

public class Mart {
private Store[] store;
private int storeNum;

public Mart(int storeNum) throws NegativeNumException {
isCheckNegativeNum(storeNum);
this.storeNum = storeNum;
initStore();
}

private void initStore() {
this.store = new Store[this.storeNum];
String str = "store";
for (int i = 0; i < store.length; i++) {
this.store[i] = new Store(str + i);
}
}

private void isCheckNegativeNum(int num) throws NegativeNumException {
if (isNegativeNum(num)) {
throw new NegativeNumException("음수를 입력하셨습니다");
}
}

private boolean isNegativeNum(int num) {
return num < 0;
}

public Store[] getStore() {
return store;
}

public void enterStore(int buyItemStore) {
System.out.println(buyItemStore + "번 째 마트에 입장했습니다");
store[buyItemStore].enter();
}

public void sellStoreItem(int buyItemStore) {
store[buyItemStore].sell();
}

public void exitStore(int storeNum) {
store[storeNum].exit();
}

public void buyStore(int storeNum) {
System.out.println("공급자가 " + storeNum + "매장에 들어갔습니다");
store[storeNum].buy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.nhnacademy.parkminsu.assignment;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* N개의 품목을 납품할 수 있음
*/
public class Producer implements Runnable {
private Mart mart;
Thread thread;
private int productItem;

public Producer(Mart mart) {
this.mart = mart;
thread = new Thread(this);
}

public void start() {
thread.start();
}

public void stop() {
thread.interrupt();
}

public void join() throws InterruptedException {
thread.join();
}

@Override
public void run() {
while (!Thread.interrupted()) {
try {
produceGood();
Thread.sleep(ThreadLocalRandom.current().nextInt(1_000, 10_000));
mart.buyStore(productItem);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println(e.getMessage());
}
}
}

public void produceGood() {
Random random = new Random();
this.productItem = random.nextInt(mart.getStore().length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.nhnacademy.parkminsu.assignment;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
* 마트에서는 N개의 품목 매장
* 생산자가 품목을 납품하기 전까지 어떤 품목인지는 알 수 없음
* 소비자는 품목별로 매장을 이용할 수 있음
*/
public class Store {

private AtomicInteger items;
private AtomicInteger currentPeople;
private final String name;
private final int storeMaxPeople = 5;

private final Semaphore semaphore;

public Store(String name) {
this.name = name;
this.items = new AtomicInteger(0);
this.currentPeople = new AtomicInteger(0);
this.semaphore = new Semaphore(storeMaxPeople);
}

/**
* 매장에 사람이 들어오면 count + 1해줌
*/
public void enter() {
try {
this.semaphore.acquire();// +1 5명 자동 lock
System.out.println("\n" + this.name + " 매장에 손님이 들어왔습니다");
this.currentPeople.addAndGet(1); // 손님이 들어오면 +1
System.out.println(this.name + " 매장 손님의 수: " + currentPeople + "\n");

} catch (InterruptedException e) {
System.out.println(e.getMessage());
}

}

public void exit() {
semaphore.release();// -1
currentPeople.addAndGet(-1);
System.out.println();
System.out.println(this.name + "매장에 손님이 나갔습니다");
System.out.println();

}

public synchronized void buy() { // 공급자한테 마트가 삼
while (items.get() > 10) {
try {
System.out.println();
System.out.println(this.name + "매장에 물품이 많습니다");
System.out.println();
wait();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
Thread.currentThread().interrupt();
}
}
if (items.get() <= 0) {
notifyAll();
}
items.addAndGet(1);
System.out.println();
System.out.println(this.name + "매장에 물품을 추가했습니다");
System.out.println(this.name + "매장 현재 물품 수: " + items.get());
System.out.println();


}

public synchronized void sell() { // 소비자 파는거
while (items.get() <= 0) {// 물품이 없을 때
try {
System.out.println();
System.out.println(this.name + "매장에 물품이 없습니다");
System.out.println();
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
notifyAll();
items.addAndGet(-1); // -1
System.out.println(this.name + "매장에 현재 남은 물품: " + items.get());
System.out.println();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nhnacademy.parkminsu.assignment.exception;

public class NegativeNumException extends Exception {
public NegativeNumException(String message) {
super(message);
}
}
48 changes: 48 additions & 0 deletions Thread/src/main/java/com/nhnacademy/parkminsu/exam/Consumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.nhnacademy.parkminsu.exam;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* 소비자는 매장에 입장 후 물건을 구매
* 매장에는 입장 인원 제한이 있으므로 인원 초과시 기다림
* 매장에 입장하면 물거은 구매하고 퇴장
* 1~10초 간격으로 구매
*/
public class Consumer implements Runnable {
private Store store;
private Thread thread;
private AtomicBoolean isCheck;


public Consumer(String name, Store store) {
this.thread = new Thread(this, name);
this.store = store;
this.isCheck = new AtomicBoolean(false);
}

public void start() {
thread.start();
}

public void stop() {
thread.interrupt();
}


@Override
public void run() {
store.enter();
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000));
store.sell();
System.out.println(thread.getName() + "가 구매했습니다");
isCheck.set(true);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println(e.getMessage());
}

store.exit();
}
}
23 changes: 23 additions & 0 deletions Thread/src/main/java/com/nhnacademy/parkminsu/exam/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nhnacademy.parkminsu.exam;

public class Main {
public static void main(String[] args) throws InterruptedException {
Store store = new Store();
String name = "손님";
int loop = 1;
Proceducer proceducer = new Proceducer(store);
proceducer.start();

while (true) {
Consumer consumer = new Consumer(name + loop, store);


consumer.start();
loop += 1;
Thread.sleep(1500);
//Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000));


}
}
}
Loading