public static void main ( String args [ ] ) throws Exception { final StringBuffer buffer = new StringBuffer ( "The lazy fox" ) ; Thread t1 = new Thread ( ) { public void run ( ) { synchronized ( buffer ) { buffer . delete ( 0 , 4 ) ; buffer . append ( " in the middle" ) ; System . err . println ( "Middle" ) ; try { Thread . sleep ( 4000 ) ; } catch ( Exception e ) { } buffer . append ( " of fall" ) ; System . err . println ( "Fall" ) ; } } } ; Thread t2 = new Thread ( ) { public void run ( ) { try { Thread . sleep ( 1000 ) ; } catch ( Exception e ) { } buffer . append ( " jump over the fence" ) ; System . err . println ( "Fence" ) ; } } ; t1 . start ( ) ; t2 . start ( ) ; t1 . join ( ) ; t2 . join ( ) ; System . err . println ( buffer ) ; }
We have more input since wait started