⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sharecell.java

📁 生产者与消费者的算法!有用到缓冲区。课程作业
💻 JAVA
字号:
/*
 * Created on 2005-5-31
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author 袁建辉
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

class HoldInteger
{
    private int sharedInt[] = {9, 9, 9, 9, 9};
    private boolean moreData = true;
    private boolean writeable = true;
    private boolean readable = false;
    private int readLoc = 0; 
    private int writeLoc = 0;
    public synchronized void push(int val){
        while(!writeable){
            try{
                System.out.println("\tWAITING TO PRODUCE " + val );
                this.wait();
            }//try
            catch(InterruptedException e){
                System.err.println("Exception: " + e.toString() );
            }//catch
        }//while()
        sharedInt[writeLoc] = val;
        readable = true;
        System.out.print("\nProduced " + val +
                				" into cell " + writeLoc );
        writeLoc = ( writeLoc + 1 ) % 5;

        System.out.print( "\twrite " + writeLoc +
                       "\tread " + readLoc );
        printBuffer(sharedInt);

        if ( writeLoc == readLoc ) {
           writeable = false;
           System.out.print( "\nBUFFER FULL" );
        }
        this.notify();
    }//push
    public synchronized int pop(){
        int val;
        while(!readable){
            try{
                System.out.println("\tWAITING TO CONSUME");
                this.wait();
            }//try
            catch(InterruptedException e){
                System.err.println("Exception: " + e.toString() );
            }//catch
        }//while()
        writeable = true;
        val = sharedInt[readLoc];
        System.out.print("\nConsumed " + val +
                				" from cell " + readLoc );
        readLoc = ( readLoc + 1 ) % 5;

        System.out.print( "\twrite " + writeLoc +
                       "\tread " + readLoc );
        
        printBuffer(sharedInt);

        if ( readLoc == writeLoc ) {
           readable = false;
           System.out.print( "\nBUFFER EMPTY" );
        }
        this.notify();
        return val;
    }//pop
    
    public void printBuffer(int buf[]){
        System.out.print("\t buffer:");
        for (int i = 0; i < buf.length; i++){
            System.out.print(" " + buf[i]);
        }//for
    }//printBuffer()
    
    public void setMoreData(boolean b){ 
        moreData = b;    
    }

    public boolean hasMoreData()
    {
       if ( moreData == false && readLoc == writeLoc ){
          return false;
       }
       else{
          return true;
       }
    }
}/*stack*/

class Producer implements Runnable
{
    private HoldInteger pHold;
    public Producer(HoldInteger s){
        pHold = s;
    }//Producer
    public void run(){
        for(int i = 0; i < 10; i++){
            try{
                Thread.sleep((int)(Math.random() * 500));
            }//try
            catch(InterruptedException e){
                System.err.println("Exception " + e.toString());
            }//catch
            pHold.push(i);
            //System.out.print( "\nProduced to " + i );
        }//for
        pHold.setMoreData(false);        
    }//run()
}/*Producer*/

class Consumer implements Runnable
{
    private HoldInteger cHold;
    public Consumer(HoldInteger s){
        cHold = s;
    } 
    public void run(){
        int val;
        while(cHold.hasMoreData()){
            try{
                Thread.sleep((int)(Math.random() * 500));
            }//try
            catch(InterruptedException e){
                System.err.println("Exception " + e.toString());
            }//catch
            val = cHold.pop();
        }//while
    }//run()
}/*Consumer*/

public class ShareCell
{
    public static void main(String args[]){
        HoldInteger s1 = new HoldInteger();
        Runnable producer = new Producer(s1);
        Runnable consumer = new Consumer(s1);
        Thread p = new Thread(producer);
        Thread c = new Thread(consumer);
        p.start();
        c.start();
    }//main()
}/*StcakTest*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -