Java Code Examples for java.util.concurrent.atomic.AtomicBoolean#lazySet()

The following examples show how to use java.util.concurrent.atomic.AtomicBoolean#lazySet() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void step3bUpdate(double[] upperBound, final int q, double[][] lowerBound,
        final int c, double[][] centroidSelfDistances, final int[] assignment, 
        final AtomicBoolean changeOccurred)
{
    //3(b)
    if (upperBound[q] > lowerBound[q][c] || upperBound[q] > centroidSelfDistances[assignment[q]][c] / 2)
    {
        double d = distance(q, c, assignment);
        lowerBound[q][c] = d;
        if (d < upperBound[q])
        {
            newDesignations[q] = c;
            
            upperBound[q] = d;
            
            changeOccurred.lazySet(true);
        }
    }
}
 
Example 2
/**
 * get returns the last value lazySet in same thread
 */
public void testGetLazySet() {
    AtomicBoolean ai = new AtomicBoolean(true);
    assertTrue(ai.get());
    ai.lazySet(false);
    assertFalse(ai.get());
    ai.lazySet(true);
    assertTrue(ai.get());
}
 
Example 3
private void item_link_q(long addr){
	int clsid = ItemUtil.getSlabsClsid(addr);
	AtomicBoolean lru_locks = JcacheContext.getLRU_Lock(clsid);
	while(!lru_locks.compareAndSet(false, true)){}
	try {
		do_item_link_q(addr);
	} finally {
		lru_locks.lazySet(false);
	}
}
 
Example 4
private void item_unlink_q(long addr) {
	int clsid = ItemUtil.getSlabsClsid(addr);
	AtomicBoolean lru_locks = JcacheContext.getLRU_Lock(clsid);
	while(!lru_locks.compareAndSet(false, true)){}
	try {
		do_item_unlink_q(addr);
	} finally {
		lru_locks.lazySet(false);
	}
}
 
Example 5
Source Project: j2objc   File: AtomicBooleanTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * get returns the last value lazySet in same thread
 */
public void testGetLazySet() {
    AtomicBoolean ai = new AtomicBoolean(true);
    assertTrue(ai.get());
    ai.lazySet(false);
    assertFalse(ai.get());
    ai.lazySet(true);
    assertTrue(ai.get());
}
 
Example 6
@Test
public void testOfferPollSemantics() throws Exception
{
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicBoolean consumerLock = new AtomicBoolean(true);
    final Queue<Integer> q = new MpscArrayQueue<Integer>(2);
    // fill up the queue
    while (q.offer(1))
    {
        ;
    }
    // queue has 2 empty slots
    q.poll();
    q.poll();

    final Val fail = new Val();
    final Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            while (!stop.get())
            {
                if (!q.offer(1))
                {
                    fail.value++;
                }

                while (!consumerLock.compareAndSet(true, false))
                {
                    ;
                }
                if (q.poll() == null)
                {
                    fail.value++;
                }
                consumerLock.lazySet(true);
            }
        }
    };
    Thread t1 = new Thread(runnable);
    Thread t2 = new Thread(runnable);

    t1.start();
    t2.start();
    Thread.sleep(1000);
    stop.set(true);
    t1.join();
    t2.join();
    assertEquals("Unexpected offer/poll observed", 0, fail.value);

}
 
Example 7
@Test
public void testOfferPollSemantics() throws Exception
{
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicBoolean consumerLock = new AtomicBoolean(true);
    final Queue<Integer> q = new MpscArrayQueue<Integer>(2);
    // fill up the queue
    while (q.offer(1))
    {
        ;
    }
    // queue has 2 empty slots
    q.poll();
    q.poll();

    final Val fail = new Val();
    final Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            while (!stop.get())
            {
                if (!q.offer(1))
                {
                    fail.value++;
                }

                while (!consumerLock.compareAndSet(true, false))
                {
                    ;
                }
                if (q.poll() == null)
                {
                    fail.value++;
                }
                consumerLock.lazySet(true);
            }
        }
    };
    Thread t1 = new Thread(runnable);
    Thread t2 = new Thread(runnable);

    t1.start();
    t2.start();
    Thread.sleep(1000);
    stop.set(true);
    t1.join();
    t2.join();
    assertEquals("Unexpected offer/poll observed", 0, fail.value);
}
 
Example 8
@Test
public void testOfferPollSemantics() throws Exception
{
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicBoolean consumerLock = new AtomicBoolean(true);
    final Queue<Integer> q = new MpscBlockingConsumerArrayQueue<>(2);
    // fill up the queue
    while (q.offer(1));

    // queue has 2 empty slots
    q.poll();
    q.poll();

    final Val fail = new Val();
    final Runnable runnable = () -> {
        while (!stop.get())
        {
            if (!q.offer(1))
            {
                fail.value++;
            }

            while (!consumerLock.compareAndSet(true, false));


            if (q.poll() == null)
            {
                fail.value++;
            }
            consumerLock.lazySet(true);
        }
    };

    Thread t1 = new Thread(runnable);
    Thread t2 = new Thread(runnable);

    t1.start();
    t2.start();
    Thread.sleep(1000);
    stop.set(true);
    t1.join();
    t2.join();
    assertEquals("Unexpected offer/poll observed", 0, fail.value);

}
 
Example 9
private void testOfferBlockSemantics(boolean withTimeout) throws Exception
{
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicBoolean consumerLock = new AtomicBoolean(true);
    final MpscBlockingConsumerArrayQueue<Integer> q = new MpscBlockingConsumerArrayQueue<>(2);
    // fill up the queue
    while (q.offer(1));

    // queue has 2 empty slots
    q.poll();
    q.poll();

    final Val fail = new Val();
    final Runnable runnable = () -> {
        while (!stop.get())
        {
            if (!q.offer(1))
            {
                fail.value++;
            }

            while (!consumerLock.compareAndSet(true, false));

            try
            {
                Integer take = withTimeout ? q.poll(1L, DAYS) : q.take();
                if (take == null)
                {
                    fail.value++;
                }
            }
            catch (InterruptedException e)
            {
                fail.value++;
            }
            consumerLock.lazySet(true);
        }
    };
    Thread t1 = new Thread(runnable);
    Thread t2 = new Thread(runnable);

    t1.start();
    t2.start();
    Thread.sleep(1000);
    stop.set(true);
    t1.join();
    t2.join();
    assertEquals("Unexpected offer/poll observed", 0, fail.value);

}