### Question:
Monitor { public synchronized T set( T newValue ) { T oldValue = value; value = newValue; notifyAll(); return oldValue; } Monitor( String description, T initialValue );  Monitor( String description ); String getDescription(); @Override String toString(); T get(); synchronized T set( T newValue ); T waitUntilEqAndSet( T desiredValue, T newValue ); synchronized T waitUntilEqAndSet( T desiredValue, int maxDelay,
		T newValue ); void waitUntilEq( T desiredValue ); synchronized void waitUntilEq( T desiredValue, int maxDelay ); T waitUntilNotEqAndSet( T undesiredValue, T newValue ); synchronized T waitUntilNotEqAndSet( T undesiredValue, int maxDelay,
		T newValue ); T waitUntilNotEq( T undesiredValue ); synchronized T waitUntilNotEq( T undesiredValue, int maxDelay );  }### Answer:
@Test public void set1() throws Exception { Monitor<Object> m = new Monitor<Object>( "a", null ); assertEquals( null, m.set( 1 ) ); assertEquals( 1, m.set( 2 ) ); assertEquals( 2, m.set( null ) ); assertEquals( null, m.set( null ) ); }
@Test public void set2() throws Exception { Monitor<Object> m = new Monitor<Object>( "a", 1 ); assertEquals( 1, m.set( 2 ) ); assertEquals( 2, m.set( null ) ); assertEquals( null, m.set( null ) ); }
@Test public void setsync() throws Exception { final Monitor<Integer> m = new Monitor<Integer>( "a", null ); final boolean[] flag = new boolean[2000]; Thread t1 = TestHelp.delay( 0, new TestHelp.DelayRunnable() { public void run() { for (int i = 0; i < 1000; i++) { Integer x = m.set( i ); if (x != null) flag[x] = true; } } } ); Thread t2 = TestHelp.delay( 0, new TestHelp.DelayRunnable() { public void run() { for (int i = 1000; i < 2000; i++) { Integer x = m.set( i ); if (x != null) flag[x] = true; } } } ); t1.start(); t2.start(); t1.join(); t2.join(); Integer x = m.set( null ); if (x != null) flag[x] = true; for (boolean b: flag) assertTrue( b ); }