자바 프로그래밍...

jabiers의 이미지

죄송합니다. 간단한 질문 하나 할께요...

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 까지 순서대로
출력이 될수 있게 하고 싶은데
스택을 건들여야 돼는거 같은데 어떻게 건들여야 될지 모르겠어요...
빠른 답변 부탁합니다. ^^

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.