java.util.concurrent.atomic.AtomicBoolean#lazySet()源码实例Demo

下面列出了java.util.concurrent.atomic.AtomicBoolean#lazySet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: JSAT   文件: ElkanKernelKMeans.java
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);
        }
    }
}
 
源代码2 项目: openjdk-jdk9   文件: AtomicBooleanTest.java
/**
 * 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());
}
 
源代码3 项目: Mycat-JCache   文件: ItemsImpl.java
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);
	}
}
 
源代码4 项目: Mycat-JCache   文件: ItemsImpl.java
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);
	}
}
 
源代码5 项目: j2objc   文件: AtomicBooleanTest.java
/**
 * 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());
}
 
源代码6 项目: JCTools   文件: QueueSanityTestMpscArray.java
@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);

}
 
@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);
}
 
@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);

}
 
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);

}