• ベストアンサー

Threadに関するの質問

このプログラムに synchronized , wait(), notify()を加えることで、 stackBody[]配列をオーバーやアンダーフローしないようにできるのでしょうか。お助け下さい! public class test { public static void main(String args[]) { Stack stack = new Stack(5); Pusher pusher = new Pusher(stack); Popper popper = new Popper(stack); pusher.start(); popper.start(); } } class Stack { private int stackSize; private int stackPointer; private int stackBody[]; public Stack(){ this(10); } public Stack(int size){ stackSize = size; stackBody = new int[size]; stackPointer = 0; } public void push(int value){ if(stackSize == stackPointer){ // } stackBody[stackPointer++] = value; } public int pop(){ if(stackPointer == 0){ // } return stackBody[--stackPointer]; } } class Pusher extends Thread { Stack s; public Pusher(Stack s) { this.s = s; } public void run() { int n, interval; for(int i = 0; i < 10; i ++) { n = (int)(100.0 * Math.random()); interval = (int)(100.0 * Math.random()); s.push(n); System.out.println("Pusher:" + n); try{ Thread.sleep(interval); }catch(InterruptedException e) {} } System.out.println("Push 終了"); } } class Popper extends Thread { Stack s; public Popper(Stack s) { this.s = s; } public void run(){ int n, interval; for(int i = 0; i < 10; i++){ interval = (int)(100.0 * Math.random()); n = s.pop(); System.out.println("Popper:" + n); try{ Thread.sleep(interval); }catch(InterruptedException e) {} } System.out.println("Popper 終了"); } }

  • Java
  • 回答数2
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • _ranco_
  • ベストアンサー率58% (126/214)
回答No.2

これなんか、いちばん標準的で教科書的なサンプルだと思います: --------------------------------------------------------- /* from: The Java Class Library: Second Edition, Volume 1 by Patrick Chan, Rosanna Lee, and Douglas Kramer */ class Stack { static final int STACK_SIZE = 3; private int[] stack_store = new int[STACK_SIZE]; private int stack_ptr = 0; // push item onto stack // If stack is full, wait until it has room synchronized public void push(int item) { while (stack_ptr >= STACK_SIZE) { try { wait(); } catch (InterruptedException e) { // ignore } } if (stack_ptr == 0) notify(); // pop was awaiting stack to fill stack_store[stack_ptr++] = item; } // pop item off top of stack // If stack is empty, wait until it has item synchronized public int pop() { while (stack_ptr == 0) { try { wait(); } catch (InterruptedException e) { // ignore } } if (stack_ptr >= STACK_SIZE) notify(); // push was awaiting stack to drain return(stack_store[--stack_ptr]); } } -----------------------------------------------------

参考URL:
http://java.sun.com/docs/books/chanlee/second_edition/vol1/

その他の回答 (1)

  • auty
  • ベストアンサー率58% (284/486)
回答No.1

これはthreadではなく、配列の添え字処理のときの不適切さ(returnの抜け)が問題です。 次のように修正してはどうでしょうか。 ----------------------------------------------- public void push(int value) { if (stackSize <= stackPointer) { return; } stackBody[stackPointer++] = value; return; } public int pop() { if (stackPointer == 0) { return -1; } return stackBody[--stackPointer]; } ----------------------------------------------- また出現回数は少ないかもしれませんがpush(int value)も値を返すようにして、オーバーフローもわかるようにしたらよいかもしれません。

関連するQ&A

  • スタックで成績表を作るプログラム。

    成績表を作りたい。Studentのクラスを要素とするクラスStackを完成させてプログラムが動作するようにせよという問題なのですがprivateの物をどうやって要素にすればよいのでしょうか? class Student { private int id; private String name; private int eng; private int math; private int kokugo; Student(int i,String nm,int e,int m,int k) { id=i;name=nm;eng=e;math=m;kokugo=k; } void show(){ System.out.println("("+id+","+name+","+ eng+","+ math+","+ kokugo+")"); } } class Stack { } class Sample { public static void main(String[] args) { Stack ss=new Stack(3); ss.push(new Student(1,"A",10,10,10)); ss.push(new Student(2,"B",20,10,10)); ss.push(new Student(3,"C",30,10,10)); ss.push(new Student(4,"D",40,10,10)); ss.pop(); ss.pop(); ss.pop(); ss.pop(); } }

    • ベストアンサー
    • Java
  • 得点を表示するクラス

    class Tokuten { private int id; private String name; private int eng; private int math; private int sci; Tokuten(int i,String nm,int e,int m,int k) { id=i;name=nm;eng=e;math=m;sci=k; } void show() { System.out.println("("+id+","+name+","+ eng+","+ math+","+ sci+")"); } } public void push(Tokuten tokuten) { if(sp!=tokutens.length) { tokuten.show(); System.out.println("push"); students[sp++]=student; } else { System.out.println("Stack is full"); } } class Sample { public static void main(String[] args) { Stack ss=new Stack(0); ss.push(new Tokuten(1,"Jiko",72,80,68)); (Tokuten)(ss.pop()).show(); } 下記のような得点の表を表示したいのですがうまくいきません set Dataという得点を受け取って各フィールドに格納するやり方が分からないのでエラーばかり出るのだと思います、どなたかソースなどお教えくださると助かります

    • ベストアンサー
    • Java
  • スレッド間の送受信のコードについて

    下記のコードは、(以前お教え頂きました)『synchronized』を使用した、スレッド間の 送受信のコードですが、その代わりに、Interruptとか、Thread.sleep()を使用した、 コードに変更してみたいと思います。 Q1)そのコードをお教え頂けないでしょうか? class T_thread { public static void main(String args[]) { new rcvInterrupt().start(); } } class genInterrupt extends Thread { private rcvInterrupt target; genInterrupt(rcvInterrupt targetx) { this.target = targetx; } public void run() { System.out.println("genInterupt start"); for (int i = 0; i < 10; i++) { while (!target.isWaited) { // rcvInterruptがsynchronizedブロックに入るまで待ち合わせ // これがないと連続してsynchronizedしてしまう可能性がある } synchronized (target) { target.notifyAll(); target.isWaited = false; System.out.println("notify"); } } } } class rcvInterrupt extends Thread { volatile boolean isWaited; public synchronized void run() { new genInterrupt(this).start(); for (int i = 0; i < 10; i++) { try { isWaited = true; wait(); System.out.println("get interrupt cnt=" + i); } catch (InterruptedException e1) { } } } } 以上

    • ベストアンサー
    • Java
  • ローカル変数にアクセスする複数のスレッドの振る舞いについて

    早速ではございますが、質問をさせていただきます。 以下のコード中のMonitorクラスを複数のスレッドが 共有して自由に入り込んでいるにも関わらす、 排他制御をしていないのにBROKENと表示されません。 final class Main { private final static int NUM = 10; public static void main(String[] args) { Monitor m = new Monitor(); UserThread[] ut = new UserThread[NUM]; Thread[] t = new Thread[NUM]; for(int i=0;i<NUM;i++) { ut[i] = new UserThread(m); t[i] = new Thread(ut[i]); t[i].start(); } } } final class UserThread implements Runnable { private final Monitor m; public UserThread(Monitor m) { this.m = m; } public void run() { System.out.println (Thread.currentThread().getName()+" BEGIN"); while(true) { this.m.func(); } } } final class Monitor { public void func() { int i1 = 0; int i2 = 0; i1++; i2++; if(i1 != i2) { System.out.println ("***** BROKEN ***** " +i1+"と"+i2); } } } 動作確認を2日間くらい行っているのですが、それでも BROKENと表示してくれません。javaの処理系によって 振る舞いがことなるかもしれませんので一概には言えませ んが、いったいなぜBROKENと表示しないのでしょうか? どうかご教授のほどよろしくお願いします。

    • ベストアンサー
    • Java
  • Javaのスレッドに関して質問です

    Assistantクラスを使い待機状態と再開を確認できるプログラムの作成 loafとrestartメソッドを設ける workメソッドが呼ばれる度loafを呼び出す Managerクラスを定義 checkメソッドを設け、Assistantをcheckし続ける loaf状態ならrestartさせる (Managerクラスはデーモンスレッド) ということなんですが、いまいちうまくいきません さぼっても復帰してくれません public class Assistant implements Runnable { private String name; private Chore c; public Assistant(String name, Chore c) { this.name = name; this.c = c; } public void run() { work(); } public void work() { while (true) { synchronized (c) { if (c.doEnd()) break; System.out.println(name + " : " + c.digest()); loaf(); } } } public synchronized void loaf() { try { c.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void restart() { c.notify(); } } public class Chore { private String name; private int step; private int id; public Chore(String name) { this.name = name; this.step = this.name.length(); this.id = 0; } public synchronized String digest() { String message = "" + id + name.charAt(id); try { Thread.sleep(500); } catch (InterruptedException ie) { } id++; return message; } public synchronized boolean doEnd() { return id >= step; } } public class Manager extends Thread { private String name; private Assistant a; public Manager(String name) { this.name = name; } public void run() { check(); } public void check() { a.restart(); } } public class Test { public static void main(String[] args) { Chore[] ch = { new Chore("掃除"), new Chore("プリント印刷"), new Chore("出欠データ入力") }; Assistant[] a = { new Assistant("あ", ch[0]), new Assistant("\tい", ch[1]), new Assistant("\t\tう", ch[2] }; Thread[] t = new Thread[a.length]; for (int i = 0; i < t.length; i++) { t[i] = new Thread(a[i]); } for (int i = 0; i < t.length; i++) { t[i].start(); } Manager m = new Manager("監査"); m.setDaemon(true); m.start(); for (int i = 0; i < t.length; i++) { try { t[i].join(); } catch( InterruptedException ie ) { } } }

    • ベストアンサー
    • Java
  • javaのスレッドの割込みについて

    javaのスレッド割込みに関する初心者の質問です。 割込みの発生元でCNTに値を設定し、割込みの受信元でCNTの値を読込み、表示しています。 下記のコードを参照ください。 Q1)CNTの値の設定と読込みで競合が発生する様なことはありませんか? 又は、処理が停滞するとか 以上、お手数をお掛けしますが、宜しくお願いします。 //割り込を発生するスレッド class threadA extends base{ private Thread target; public threadA(Thread targetA){ target = targetA; } public void run(){ for(int i = 0; i < 8;i++){ try{ System.out.print("."); Thread.sleep(1); CNT=i; target.interrupt(); }catch(InterruptedException e){ System.out.println("今割り込まれました: thread"); } } } } //スレッドからの割り込みを受信するルーチン class Sample108{ public static void main(String args[]){ //for(int i=0; i<2; i++){ classA obj = new classA(); obj.threadStart();//割り込みを受けるルーチン、この中で、割り込みを発生するルーチンを起動している //} } } //割り込みを受けるルーチン class classA extends base{ void threadStart(){ System.out.println("Start: main****"); threadA obj = new threadA(Thread.currentThread()); obj.start(); //Thread起動 //mainのルーチン for(int j = 0;j < 20; j++){ try{ Thread.sleep(1); System.out.print("*"); }catch(InterruptedException e){ System.out.println("今割り込まれました: classA CNT="+CNT); } } } } 以上

    • ベストアンサー
    • Java
  • Javaの文字列配列について質問です

    あは1行目の1つめです。 かは2行目の1つめです。 さは3行目の1つめです。 と表示させたいのですが、 Exception in thread "main" java.lang.NullPointerException at Nihongo.main(Nihongo.java:22) と出てきて出来ません。 どこが悪いのでしょうか? 22行目は「s[i].easyNihongo();」です。 class Hiragana { String boin; int num; int kaz; Hiragana(String b, int n, int k){ boin = b; num = n; kaz = k; } void easyNihongo(){ System.out.println(boin+"は"+num+"行目の"+kaz+"つ目です。"); } } class Nihongo{ public static void main(String [] args){ String aiueo[] = {"あ", "か", "さ"}; Hiragana [] s = new Hiragana[3]; for(int i=0; i<s.length; i++){ s[i] = new Hiragana(aiueo[i], 1, i++); s[i].easyNihongo(); } } }

    • ベストアンサー
    • Java
  • synchronizedが解りません。

    synchronized( a ) { a.add(); } ブロック文で aオブジェクトをロックしてるはずなんですが 結果を見ると出来てないようです なぜうまくいかないんでしょうか? 教えてください。 class Test extends Thread { feet a = new feet(); public static void main( String args[] ){ for( int i = 0; i < 3; i++ ){ Test b = new Test(); b.start(); } } public void run() { synchronized( a ) { a.add(); } } } class feet{ int n, m; void add(){ n++; System.out.println( n + " " + m ); try { Thread.sleep( 1000 ); }catch( InterruptedException err ){ } m++; System.out.println( n + " " + m ); } }

    • ベストアンサー
    • Java
  • スレッド動かず

    本の真似をして、スレッドを書いてみたんですがに何も起こりませんでした。全く分かりません。どうすれば良いでしょうか? public class Frame1 extends Frame implements Runable{ ................. ................. Thread kick = new Thread(this); int time; public void start() { if (kick ==null) kick.start(); } public void run() { while (kick == Thread.currentThread()) { time++; String s = Integer.toString(time); textArea1.append( s + "\r\n"); try{ Thread.sleep(1000); }catch(InterruptedException e){} } }

    • ベストアンサー
    • Java
  • スレッドの実行結果表示順について

    java学習超初心者です。一人で勉強しています。 以下のコードの実行結果に対して質問があります。 class MyThread extends Thread{ public void run(){ for (int i=1;i<=8; i++){ System.out.println("*************************サブスレッドも動いています********" + i); } } } public class JaThrd1{ public static void main (String args[]){ MyThread aa = new MyThread(); aa.start(); for(int i=1; i<=12; i++){ System.out.println("メインスレッドで仕事中です" + i); } } } 実行結果が↓です *************************サブスレッドも動いています********1 メインスレッドで仕事中です1 *************************サブスレッドも動いています********2 メインスレッドで仕事中です2 *************************サブスレッドも動いています********3 メインスレッドで仕事中です3 *************************サブスレッドも動いています********4 メインスレッドで仕事中です4 *************************サブスレッドも動いています********5 メインスレッドで仕事中です5 *************************サブスレッドも動いています********6 メインスレッドで仕事中です6 *************************サブスレッドも動いています********7 メインスレッドで仕事中です7 *************************サブスレッドも動いています********8 メインスレッドで仕事中です8 メインスレッドで仕事中です9 メインスレッドで仕事中です10 メインスレッドで仕事中です11 メインスレッドで仕事中です12 なぜこのような順番の結果になるのでしょうか? 交互に表示されることがわかりません。

    • ベストアンサー
    • Java

専門家に質問してみよう