자바 프로그래밍...
죄송합니다. 간단한 질문 하나 할께요...
class SyncStack {
private int index = 0;
private int [] buffer = new int[6]; // 스택의 크기는 6개// 스택으로부터 꺼냄
public synchronized int pop() { //
while (index == 0) { // 하나도 없으면 기다림.
try {
this.wait();
} catch (InterruptedException e) { }
}
this.notify(); // 아니면, 수행
index--;
System.out.println("소비자########## : 소비" + buffer);
return buffer[2];
}
// 스택에 넣음
public synchronized void push(int c) {
while (index == buffer.length) { // 가득 찼으면 기다림.
try {
this.wait();
} catch (InterruptedException e) { }
}
this.notify(); // 아니면, 수행
int buffer = c;
System.out.println("생산자########## : 생산" + c);
index++;
}
// 생산자 클래스 (스택에 값을 계속 넣음)
class Producer implements Runnable {
SyncStack theStack;
public Producer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
for (int i = 0; i < 10; i++) {
c = (int)(Math.random() * 26 + 'A');
theStack.push(c);
try {
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
// 소비자 클래스 (스택에서 값을 계속 가져감)
// 생산자보다 소비자의 속도가 늦어서 스택에 쌓이게 함.
class Consumer implements Runnable {
SyncStack theStack;
public Consumer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
for (int i=0; i<10; i++) {
c = theStack.pop();
try {
Thread.sleep((int)(Math.random() * 300));
} catch (InterruptedException e) { }
}
}
}
}
public class ProducerConsumer {
public static void main(String args[]) {
SyncStack stack = new SyncStack();
Runnable producer = new Producer(stack);
Runnable consumer = new Consumer(stack);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
class SyncStack {
private int index = 0;
private int [] buffer = new int[6]; // 스택의 크기는 6개// 스택으로부터 꺼냄
public synchronized int pop() { //
while (index == 0) { // 하나도 없으면 기다림.
try {
this.wait();
} catch (InterruptedException e) { }
}
this.notify(); // 아니면, 수행
index--;
System.out.println("소비자########## : 소비" + buffer);
return buffer[2];
}
// 스택에 넣음
public synchronized void push(int c) {
while (index == buffer.length) { // 가득 찼으면 기다림.
try {
this.wait();
} catch (InterruptedException e) { }
}
this.notify(); // 아니면, 수행
int buffer = c;
System.out.println("생산자########## : 생산" + c);
index++;
}
// 생산자 클래스 (스택에 값을 계속 넣음)
class Producer implements Runnable {
SyncStack theStack;
public Producer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
for (int i = 0; i < 10; i++) {
c = (int)(Math.random() * 26 + 'A');
theStack.push(c);
try {
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
// 소비자 클래스 (스택에서 값을 계속 가져감)
// 생산자보다 소비자의 속도가 늦어서 스택에 쌓이게 함.
class Consumer implements Runnable {
SyncStack theStack;
public Consumer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
for (int i=0; i<10; i++) {
c = theStack.pop();
try {
Thread.sleep((int)(Math.random() * 300));
} catch (InterruptedException e) { }
}
}
}
}
public class ProducerConsumer {
public static void main(String args[]) {
SyncStack stack = new SyncStack();
Runnable producer = new Producer(stack);
Runnable consumer = new Consumer(stack);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
이 소스를 실행하면
---------- Java ----------
생산자########## : 생산75
소비자########## : 소비[I@1b67f74
생산자########## : 생산77
생산자########## : 생산88
생산자########## : 생산67
생산자########## : 생산78
소비자########## : 소비[I@1b67f74
생산자########## : 생산73
생산자########## : 생산88
소비자########## : 소비[I@1b67f74
생산자########## : 생산78
소비자########## : 소비[I@1b67f74
생산자########## : 생산72
생산자########## : 생산77
소비자########## : 소비[I@1b67f74
소비자########## : 소비[I@1b67f74
소비자########## : 소비[I@1b67f74
소비자########## : 소비[I@1b67f74
소비자########## : 소비[I@1b67f74
소비자########## : 소비[I@1b67f74
Normal Termination
출력 완료 (2초 경과).
이런게 출력이 됩니다.
여기서
생산자에게는 생산 0~9 까지 순서대로
소비자도 소비 0~9 까지 순서대로
출력이 될수 있게 하고 싶은데
스택을 건들여야 돼는거 같은데 어떻게 건들여야 될지 모르겠어요...
빠른 답변 부탁합니다. ^^
댓글 달기