// Sync1.java

// Example of two synchronized threads (mutator and accessor)
// by Mark Weiss
// modified by Kip Irvine, 1/22/03

class ThreeObjs
{
    synchronized public void swap( )
    {
    	System.out.println("Entering swap()");
        int tmp = a;
        a = b;
        b = c;
        c = tmp;
    	System.out.println("Leaving swap()");
    }

    synchronized public void print( )
    {
        System.out.println( a + " " + b + " " + c );
    }
    
    private int a = 0;
    private int b = 1111;
    private int c = 88888888;
}

class Sync extends Thread
{
    static ThreeObjs obj = new ThreeObjs( );

    public void run( )
    {
        for( int i = 0; i < 20000000 ; i++ )
        {
            obj.swap( );
            if( i % 500000 == 0 )
            {
                obj.print( );
                try { sleep( 1 ); }
                catch( InterruptedException e ) { }
            }
        }
    }

    public static void main( String [ ] args )
    {
        Thread t1 = new Sync( );
        Thread t2 = new Sync( );

        t1.start( );
        t2.start( );
    }
}
