多线程

public class CubbyHole
{
    private Object slot;
    public CubbyHole()
    {
        slot = null;
    }
    public synchronized void putIn( Object obj )
     throws InterruptedException
    {
        print( "in putIn() - entering" );
        while( slot!=null )
        {
            print( "in putIn() - occupied, about to wait()" );
            wait();
            print( "in putIn() - notified, back from wait()" );
        }
        slot = obj;
        print( "in putIn() - filled slot, about to notifyAll()" );
        notifyAll();
        print( "in putIn() - leaving" );
    }
    public synchronized Object takeOut()
     throws InterruptedException
    {
        print( "in takeOut() - entering" );
        while( slot==null )
        {
            print( "in takeOut() - empty, about to wait()" );
            wait();
            print( "in takeOut() - notified, back from wait()" );
        }
        Object obj = slot;
        slot = null;
        print( "in takeOut() - emptied slot, about to notifyAll()" );
        notifyAll();
        print( "in takeOut() - leaving" );
        return obj;
    }
    private static void print( String msg )
    {
        String name = Thread.currentThread().getName();
        System.out.println( name + ":" + msg );
    }
   
}
/*********************************************************/

public class CubbyHoleMain
{
    private static void print( String msg )
    {
        String name = Thread.currentThread().getName();
        System.out.println( name + ":" + msg );
    }

    public static void main(String[] args)
    {
        final CubbyHole ch = new CubbyHole();
        Runnable runA = new Runnable()
        {
            public void run()
            {
                try
                {
                    String str;
                    Thread.sleep( 500 );
                    str = "multithread";
                    ch.putIn( str );
                    print( "in run() - just put in: '"+ str + "'" );
                   
                    str = "programming";
                    ch.putIn( str );
                    print( "in run() - just put in: '" + str + "'" );
                   
                    str = "with Java";
                    ch.putIn( str );
                    print( "in run() - just put in: '" + str + "'" );
                   
                }catch( InterruptedException x )
                {
                    x.printStackTrace();
                }
            }
           
        }
        ;
       
        /
        Runnable runB = new Runnable()
        {
            public void run()
            {
                try
                {
                    Object obj;
                    obj = ch.takeOut();
                    print( "in run() - just took out: '" + obj + "'" );
                    Thread.sleep( 500 );
                   
                    obj = ch.takeOut();
                    print( "in run() - just took out: '" + obj + "'" );
                   
                    obj = ch.takeOut();
                    print( "in run() - just took out: '" + obj + "'" );
                   
                }catch( InterruptedException x )
                {
                    x.printStackTrace();
                }
            }
           
        }
        ;
       
        Thread threadA = new Thread( runA, "threadA" );
        threadA.start();
       
        Thread threadB = new Thread( runB, "threadB" );
        threadB.start();
       
    }
   
}
/****************************************************/

threadB:in takeOut() - entering
threadB:in takeOut() - empty, about to wait()
threadA:in putIn() - entering
threadA:in putIn() - filled slot, about to notifyAll()
threadA:in putIn() - leaving
threadB:in takeOut() - notified, back from wait()
threadB:in takeOut() - emptied slot, about to notifyAll()
threadB:in takeOut() - leaving
threadB:in run() - just took out: 'multithread'
threadA:in run() - just put in: 'multithread'
threadA:in putIn() - entering
threadA:in putIn() - filled slot, about to notifyAll()
threadA:in putIn() - leaving
threadA:in run() - just put in: 'programming'
threadA:in putIn() - entering
threadA:in putIn() - occupied, about to wait()
threadB:in takeOut() - entering
threadB:in takeOut() - emptied slot, about to notifyAll()
threadB:in takeOut() - leaving
threadB:in run() - just took out: 'programming'
threadB:in takeOut() - entering
threadB:in takeOut() - empty, about to wait()
threadA:in putIn() - notified, back from wait()
threadA:in putIn() - filled slot, about to notifyAll()
threadA:in putIn() - leaving
threadA:in run() - just put in: 'with Java'
threadB:in takeOut() - notified, back from wait()
threadB:in takeOut() - emptied slot, about to notifyAll()
threadB:in takeOut() - leaving
threadB:in run() - just took out: 'with Java'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值