// Sync3.java

// two separate objects

class ThreeObjs
{
    synchronized public void swap( )
    {
        int tmp = a;
        a = b;
        b = c;
        Thread.currentThread( ).yield( );
        c = tmp;
    }

    synchronized public void print( )
    {
        System.out.println( idnum + ": " + a + " " + b + " " + c );
        Thread.currentThread( ).yield( );
    }
    
    private int a = 0;
    private int b = 1111;
    private int c = 88888888;
    private int idnum = count++;
    private static int count = 0;
}

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

    public void run( )
    {
        for( int i = 0; i < 20000000 ; i++ )
        {
            obj1.swap( );
			obj2.swap( );
            if( i % 80000 == 0 )
            {
                obj1.print( );
                obj2.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( );
    }
}
