Code example for AtomicInteger

Methods: compareAndSet, get

0
      */ 
 
    @Test 
    public void testCompareAndSet() throws InterruptedException 
    { 
        final AtomicInteger ai = new AtomicInteger(1);
        Thread t = new Thread(new Runnable()
        { 
            public void run() 
            { 
                while (!ai.compareAndSet(2, 3)) Thread.yield();
            } 
        }); 
 
        t.start();
        assertTrue(ai.compareAndSet(1, 2));
        t.join(2500);
        assertFalse(t.isAlive());
        assertEquals(ai.get(), 3);
    } 
 
 
}