多线程

来源:互联网 发布:centos7 3306端口打开 编辑:程序博客网 时间:2024/05/17 07:21

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'

原创粉丝点击