条件変数を使った環状バッファ(Java)

並列分散ソフトウェア

                                       電子・情報工学系
                                       新城 靖
                                       <yas@is.tsukuba.ac.jp>

このページは、次の URL にあります。
http://www.hlla.is.tsukuba.ac.jp/~yas/sie/pdsoft-2001/2002-01-17 cirq-buffer-java.html
あるいは、次のページから手繰っていくこともできます。
http://www.hlla.is.tsukuba.ac.jp/~yas/sie/
http://www.is.tsukuba.ac.jp/~yas/index-j.html
http://www.hlla.is.tsukuba.ac.jp/~yas/index-j.html

■条件変数を使った環状バッファ(Java)


----------------------------------------------------------------------
   1: 
   2: /*
   3:  * CircularBuffer.java -- Java による環状バッファ
   4:  * このファイルは次の場所にあります。
   5:  *   http://www.hlla.is.tsukuba.ac.jp/~yas/sie/pdsoft-2001/examples/CircularBuffer.java
   6:  * wget コマンドでコピーできます。
   7:  * Start: 2002/01/17 01:09:36
   8: */
   9: 
  10: class CircularBuffer
  11: {
  12:     static final int BUFFER_SIZE = 4 ;
  13:     int rp ;            // 読み出す位置
  14:     int wp ;            // 書き込む位置
  15:     int data[];         // データを保存する場所
  16:     int used ;          // バッファ内の要素数
  17:     CircularBuffer()
  18:     {
  19:         data = new int[BUFFER_SIZE];
  20:         rp = 0 ;
  21:         wp = 0 ;
  22:     }
  23: 
  24:     public synchronized void put( int x ) throws InterruptedException
  25:     {
  26:         while( used == data.length )
  27:             wait();
  28:         data[ wp++ ] = x ;
  29:         if( wp == data.length )
  30:             wp = 0 ;
  31:         if( used++ == 0 )
  32:             notifyAll();
  33:     }
  34: 
  35:     public synchronized int get() throws InterruptedException
  36:     {
  37:         int x ;
  38:         while( used == 0 )
  39:             wait();         
  40:         x = data[ rp++ ] ;
  41:         if( rp >= data.length )
  42:             rp = 0 ;
  43:         if( used-- == data.length )
  44:             notifyAll();
  45:         return( x );
  46:     }
  47: 
  48:     static void thread_A( CircularBuffer b )    // Producer
  49:     {                           
  50:         int i,x ;
  51:         try {
  52:             for( i = 0 ; i<10 ; i++ )
  53:             {
  54:                 x = i ;
  55:                 System.out.println("thread_A(): put( "+x+" )");
  56:                 b.put( x );
  57:             }
  58:         }
  59:         catch( InterruptedException e )
  60:         {
  61:             System.err.println("thread_A(): Interrupted");
  62:         }
  63:     }
  64: 
  65:     static void thread_B( CircularBuffer b )    // Consumer
  66:     {
  67:         int i, x ;
  68:         try {
  69:             for( i = 0 ; i<10 ; i++ )
  70:             {
  71:                 x = b.get();
  72:                 System.out.println("thread_B(): get() "+x+".");
  73:             }
  74:         }
  75:         catch( InterruptedException e )
  76:         {
  77:             System.err.println("thread_B(): Interrupted");
  78:         }
  79:     }
  80: 
  81:     static void main(String argv[])
  82:     {
  83:         final CircularBuffer b = new CircularBuffer();
  84:         Thread t1 = new Thread( new Runnable()
  85:                                  { public void run() {thread_A(b);} } );
  86:         t1.start();
  87:         Thread t2 = new Thread( new Runnable()
  88:                                  { public void run() {thread_B(b);} } );
  89:         t2.start();
  90:         try {
  91:             t1.join();
  92:             t2.join();
  93:         }
  94:         catch( InterruptedException e )
  95:         {
  96:             System.err.println("main(): Interrupted");
  97:         }
  98:     }
  99: }
----------------------------------------------------------------------


↑[もどる] ←[1月10日] ・[1月17日] →[1月24日]
Last updated: 2002/01/17 03:01:23
Yasushi Shinjo / <yas@is.tsukuba.ac.jp>