/** * Illustrates that statics and each instance have different monitors. */ class Foo { synchronized public static void staticMethod( ) throws InterruptedException { System.out.println( "Entering static method" ); Thread.sleep( 10000 ); System.out.println( "Leaving static method" ); } synchronized public void instanceMethod( ) throws InterruptedException { System.out.println( "Entering instance method " + this ); Thread.sleep( 7000 ); System.out.println( "Leaving instance method " + this ); } private int id = count++; private static int count = 0; public String toString( ) { return "Foo #" + id; } } class MonitorThread extends Thread { public MonitorThread( Foo f ) { this.f = f; } public void run( ) { try { f.instanceMethod( ); f.staticMethod( ); } catch( InterruptedException e ) { } } private Foo f; } class StaticsAndThreads { public static void main( String[] args ) { Foo f0 = new Foo( ); Foo f1 = new Foo( ); Thread t1 = new MonitorThread( f0 ); Thread t2 = new MonitorThread( f1 ); Thread t3 = new MonitorThread( f0 ); t3.setPriority( Thread.MAX_PRIORITY ); t3.setPriority( Thread.MIN_PRIORITY ); t1.start( ); t2.start( ); t3.start( ); } }