// Sync2.java

// Synchronize swap(), but not print()
// better performance, printed values can be temporarily incorrect
// by Mark Weiss

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

    // proceeds without waiting for the monitor
    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 % 80000 == 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( );
    }
}
