java.util.concurrent.atomic.AtomicMarkableReference Java Examples

The following examples show how to use java.util.concurrent.atomic.AtomicMarkableReference. 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 Project: Oak   Author: yahoo   File: Chunk.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This constructor is only used internally to instantiate a Chunk without a creator and a min-key.
 * The caller should set the creator and min-key before returning the Chunk to the user.
 */
private Chunk(int maxItems, AtomicInteger externalSize, MemoryManager memoryManager, OakComparator<K> comparator,
              OakSerializer<K> keySerializer, OakSerializer<V> valueSerializer, ValueUtils valueOperator) {
    this.maxItems = maxItems;
    this.externalSize = externalSize;
    this.comparator = comparator;
    this.entrySet = new EntrySet<>(memoryManager, maxItems, keySerializer, valueSerializer, valueOperator);
    // if not zero, sorted count keeps the entry index of the last
    // subsequent and ordered entry in the entries array
    this.sortedCount = new AtomicInteger(0);
    this.minKey = new KeyBuffer();
    this.creator = new AtomicReference<>(null);
    this.state = new AtomicReference<>(State.NORMAL);
    this.next = new AtomicMarkableReference<>(null, false);
    this.pendingOps = new AtomicInteger();
    this.rebalancer = new AtomicReference<>(null); // to be updated on rebalance
    this.statistics = new Statistics();
}
 
Example #2
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AtomicMarkableReferenceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * get returns the last values of reference and mark set
 */
public void testGetSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(two, false);
    assertSame(two, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(one, true);
    assertSame(one, ai.getReference());
    assertTrue(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #3
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AtomicMarkableReferenceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet succeeds in changing values if equal to expected reference
 * and mark else fails
 */
public void testCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);

    assertFalse(ai.compareAndSet(two, m3, true, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #4
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AtomicMarkableReferenceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for reference value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(two, three, false, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, two, false, false));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #5
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AtomicMarkableReferenceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for mark value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads2() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(one, one, true, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, one, false, true));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #6
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AtomicMarkableReferenceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing values when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #7
Source Project: TAOMP   Author: MottoX   File: LockFreeList.java    License: MIT License 6 votes vote down vote up
@Override
public boolean add(T item) {
    int key = item.hashCode();
    while (true) {
        Window window = find(head, key);
        Node<T> pred = window.pred, curr = window.curr;
        if (curr.key == key) {
            return false;
        } else {
            Node<T> node = new Node<>(item);
            node.next = new AtomicMarkableReference<>(curr, false);
            if (pred.next.compareAndSet(curr, node, false, false)) {
                return true;
            }
        }
    }
}
 
Example #8
Source Project: lin-check   Author: devexperts   File: LockFreeDeque.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
public void pushRight(T x) {
    Node node = new Node(x, null, null);
    Node next = tail;
    Node prev = next.prev.getReference();
    while (true) {
        if (!prev.next.compareAndSet(next, next, false, false)) {
            // concurrent push inserted -> get new prev
            prev = helpInsert(prev, next, "concurrentPushRight");
            continue;
        }
        // 0 push step
        node.prev = new AtomicMarkableReference<>(prev, false);
        node.next = new AtomicMarkableReference<>(next, false);
        // 1 push step
        if (prev.next.compareAndSet(next, node, false, false)) {
            break;
        }
    }
    // 2 push step
    pushCommon(node, next);
}
 
Example #9
Source Project: lin-check   Author: devexperts   File: LockFreeDeque.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
public void pushLeft(T x) {
    Node node = new Node(x, null, null);
    Node prev = head;
    Node next = prev.next.getReference();
    while (true) {
        if (!prev.next.compareAndSet(next, next, false, false)) {
            next = prev.next.getReference();
            continue;
        }
        node.prev = new AtomicMarkableReference<>(prev, false);
        node.next = new AtomicMarkableReference<>(next, false);

        if (prev.next.compareAndSet(next, node, false, false)) {
            break;
        }
    }
    pushCommon(node, next);
}
 
Example #10
Source Project: j2objc   Author: google   File: AtomicMarkableReferenceTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * get returns the last values of reference and mark set
 */
public void testGetSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(two, false);
    assertSame(two, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(one, true);
    assertSame(one, ai.getReference());
    assertTrue(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #11
Source Project: j2objc   Author: google   File: AtomicMarkableReferenceTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet succeeds in changing values if equal to expected reference
 * and mark else fails
 */
public void testCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);

    assertFalse(ai.compareAndSet(two, m3, true, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #12
Source Project: j2objc   Author: google   File: AtomicMarkableReferenceTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for reference value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(two, three, false, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, two, false, false));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #13
Source Project: j2objc   Author: google   File: AtomicMarkableReferenceTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for mark value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads2() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(one, one, true, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, one, false, true));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #14
Source Project: j2objc   Author: google   File: AtomicMarkableReferenceTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing values when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #15
public static void main(String[] args) throws InterruptedException {

        final AtomicMarkableReference<String> amr = new AtomicMarkableReference<>(INIT_TEXT, false);

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(Math.abs((int) (Math.random() * 100)));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    String name = Thread.currentThread().getName();
                    if (amr.compareAndSet(INIT_TEXT, name, amr.isMarked(), !amr.isMarked())) {
                        System.out.println(Thread.currentThread().getName() + " 修改了对象!");
                        System.out.println("新的对象为:" + amr.getReference());
                    }
                }
            });
        }

        executorService.shutdown();
        executorService.awaitTermination(3, TimeUnit.SECONDS);
    }
 
Example #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AtomicMarkableReferenceTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * constructor initializes to given reference and mark
 */
public void testConstructor() {
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
    AtomicMarkableReference a2 = new AtomicMarkableReference(null, true);
    assertNull(a2.getReference());
    assertTrue(a2.isMarked());
}
 
Example #17
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AtomicMarkableReferenceTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * attemptMark succeeds in single thread
 */
public void testAttemptMark() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertFalse(ai.isMarked());
    assertTrue(ai.attemptMark(one, true));
    assertTrue(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #18
Source Project: twill   Author: apache   File: RewatchOnExpireWatcher.java    License: Apache License 2.0 5 votes vote down vote up
RewatchOnExpireWatcher(ZKClient client, ActionType actionType, String path, Watcher delegate) {
  this.client = client;
  this.actionType = actionType;
  this.path = path;
  this.delegate = delegate;
  this.lastResult = new AtomicMarkableReference<Object>(null, false);
}
 
Example #19
Source Project: lin-check   Author: devexperts   File: LockFreeDeque.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private void pushCommon(Node node, Node next) {
    while (true) {
        AtomicMarkableReference<Node> link1 = next.prev;
        if (link1.isMarked() || !node.next.compareAndSet(next, next, false, false)) {
            break;
        }
        if (next.prev.compareAndSet(link1.getReference(), node, false, false)) {
            if (node.prev.isMarked()) {
                helpInsert(node, next, "pushCommon");
            }
            break;
        }
    }
}
 
Example #20
Source Project: lin-check   Author: devexperts   File: LockFreeDeque.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private void markPrev(Node node) {
    while (true) {
        AtomicMarkableReference<Node> link1 = node.prev;
        if (link1.isMarked() || node.prev.compareAndSet(link1.getReference(), link1.getReference(), false, true)) {
            break;
        }
    }
}
 
Example #21
Source Project: j2objc   Author: google   File: AtomicMarkableReferenceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * constructor initializes to given reference and mark
 */
public void testConstructor() {
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
    AtomicMarkableReference a2 = new AtomicMarkableReference(null, true);
    assertNull(a2.getReference());
    assertTrue(a2.isMarked());
}
 
Example #22
Source Project: j2objc   Author: google   File: AtomicMarkableReferenceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * attemptMark succeeds in single thread
 */
public void testAttemptMark() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertFalse(ai.isMarked());
    assertTrue(ai.attemptMark(one, true));
    assertTrue(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #23
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void givenMarkValueAsTrue_whenUsingIsMarkedMethod_thenMarkValueShouldBeTrue() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #24
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void givenMarkValueAsFalse_whenUsingIsMarkedMethod_thenMarkValueShouldBeFalse() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, false);

    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #25
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void whenUsingGetReferenceMethod_thenCurrentReferenceShouldBeReturned() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Assertions.assertEquals(employee, employeeNode.getReference());
}
 
Example #26
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void whenUsingGetMethod_thenCurrentReferenceAndMarkShouldBeReturned() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    boolean[] markHolder = new boolean[1];
    Employee currentEmployee = employeeNode.get(markHolder);

    Assertions.assertEquals(employee, currentEmployee);
    Assertions.assertTrue(markHolder[0]);
}
 
Example #27
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void givenNewReferenceAndMark_whenUsingSetMethod_thenCurrentReferenceAndMarkShouldBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Employee newEmployee = new Employee(124, "John");
    employeeNode.set(newEmployee, false);

    Assertions.assertEquals(newEmployee, employeeNode.getReference());
    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #28
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void givenTheSameObjectReference_whenUsingAttemptMarkMethod_thenMarkShouldBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Assertions.assertTrue(employeeNode.attemptMark(employee, false));
    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #29
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void givenDifferentObjectReference_whenUsingAttemptMarkMethod_thenMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee expectedEmployee = new Employee(123, "Mike");

    Assertions.assertFalse(employeeNode.attemptMark(expectedEmployee, false));
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #30
Source Project: tutorials   Author: eugenp   File: AtomicMarkableReferenceUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
void givenCurrentReferenceAndCurrentMark_whenUsingCompareAndSet_thenReferenceAndMarkShouldBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertTrue(employeeNode.compareAndSet(employee, newEmployee, true, false));
    Assertions.assertEquals(newEmployee, employeeNode.getReference());
    Assertions.assertFalse(employeeNode.isMarked());
}