디자인 모델 - 독자, 작성 자

4238 단어
/* 
 *        
 *  
 * */
class Buffer
{
    public String buffer[] = new String[10];//            
    public boolean flag = false;//            
    public int i=0;//             
}
class Writer implements Runnable //         
{
    private Buffer buffer;
    public Writer(Buffer buffer)
    {
        this.buffer = buffer;
    }
    @Override
    public void run()
    {
        while (true)//                
        {
            synchronized (buffer)//         ,            ,           
            {
                while (buffer.flag)//      flag=true,                      ,      ,    ,                
                {
                    try
                    {
                        buffer.wait();
                    } catch (InterruptedException ex)
                    {
                        System.out.println(ex.getMessage());
                        ex.printStackTrace(System.out);
                    }
                }
                if (buffer.i == buffer.buffer.length)
                {
                    System.out.println("     ,     ");
                    buffer.buffer = new String[10];
                    buffer.i = 0;
                }
                buffer.buffer[buffer.i] = "   " + buffer.i;//                   
                System.out.println(Thread.currentThread().getName()+" " + buffer.i + "  :" + buffer.buffer[buffer.i]);
                buffer.i = buffer.i + 1;//         
                buffer.flag = true;
                buffer.notifyAll();
            }
            try
            {
                Thread.sleep(100);
            } catch (Exception ex)
            {
                System.out.println(ex.getMessage());
                ex.printStackTrace(System.out);
            }
        }
    }
}
class Reader implements Runnable
{
    private Buffer buffer;
    public Reader(Buffer buffer)
    {
        this.buffer = buffer;
    }
    @Override
    public void run()
    {
        while (true) //               
        {
            synchronized (buffer)
            {
                while (!buffer.flag)
                {
                    try
                    {
                        buffer.wait();
                    } catch (InterruptedException ex)
                    {
                        System.out.println(ex.getMessage());
                        ex.printStackTrace(System.out);
                    }
                }
                System.out.print(Thread.currentThread().getName()+"   :");
                for (int i = 0; i < buffer.i; i++)
                {
                    System.out.print(buffer.buffer[i]);
                }
                System.out.println();
                buffer.flag = false;
                buffer.notifyAll();
            }
            try
            {
                Thread.sleep(100);//         ,           ,             ,         
            } catch (InterruptedException ex)
            {
                System.out.println(ex.getMessage());
                ex.printStackTrace(System.out);
            }  
        }
    }
}
/* 
 *                                          ,         ,          
 *             ,            ,               ,               --- 
 *                         ,          ,          ,                  
 *                      ,          ,    wait  ,               ,         
 *                  ,          ,                      ,           ,        
 *          if(buffer.flag)    wait          ,            ,             ,       ,           , 
 *                      ,     if(buffer.flag)   while(buufer.flag) buffer.notify    buffer.notifyAll();             
 *       
 *              while(flag) notifyAll    ,  ,                ,      ,                 
 *                       
 */
public class ReaderandWriter
{
    public static void main(String args[])
    {
        Buffer buffer = new Buffer();
        Thread t1 = new Thread(new Writer(buffer));
        Thread t2 = new Thread(new Writer(buffer));
        Thread t3 = new Thread(new Reader(buffer));
        Thread t4 = new Thread(new Reader(buffer));
        t1.start();
        t2.start();  
        t3.start();
        t4.start();
    }
}

좋은 웹페이지 즐겨찾기