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

The following examples show how to use java.util.concurrent.atomic.AtomicBoolean#lazySet() . 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
Source File: ElkanKernelKMeans.java    From JSAT with GNU General Public License v3.0 6 votes vote down vote up
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
Source File: AtomicBooleanTest.java    From openjdk-jdk9 with GNU General Public License v2.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 3
Source File: ItemsImpl.java    From Mycat-JCache with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: ItemsImpl.java    From Mycat-JCache with GNU General Public License v2.0 5 votes vote down vote up
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 File: AtomicBooleanTest.java    From j2objc with 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
Source File: QueueSanityTestMpscArray.java    From JCTools with Apache License 2.0 4 votes vote down vote up
@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
Source File: QueueSanityTestMpscArrayExtended.java    From JCTools with Apache License 2.0 4 votes vote down vote up
@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
Source File: QueueSanityTestMpscBlockingConsumerArrayExtended.java    From JCTools with Apache License 2.0 4 votes vote down vote up
@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
Source File: QueueSanityTestMpscBlockingConsumerArrayExtended.java    From JCTools with Apache License 2.0 4 votes vote down vote up
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);

}