当前位置: 首页>>代码示例>>Java>>正文


Java AtomicInteger.compareAndSet方法代码示例

本文整理汇总了Java中java.util.concurrent.atomic.AtomicInteger.compareAndSet方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicInteger.compareAndSet方法的具体用法?Java AtomicInteger.compareAndSet怎么用?Java AtomicInteger.compareAndSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.atomic.AtomicInteger的用法示例。


在下文中一共展示了AtomicInteger.compareAndSet方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: remove

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Removes a number of occurrences of the specified element from this multiset. If the multiset
 * contains fewer than this number of occurrences to begin with, all occurrences will be removed.
 *
 * @param element the element whose occurrences should be removed
 * @param occurrences the number of occurrences of the element to remove
 * @return the count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative
 */
/*
 * TODO(cpovirk): remove and removeExactly currently accept null inputs only
 * if occurrences == 0. This satisfies both NullPointerTester and
 * CollectionRemoveTester.testRemove_nullAllowed, but it's not clear that it's
 * a good policy, especially because, in order for the test to pass, the
 * parameter must be misleadingly annotated as @Nullable. I suspect that
 * we'll want to remove @Nullable, add an eager checkNotNull, and loosen up
 * testRemove_nullAllowed.
 */
@Override public int remove(@Nullable Object element, int occurrences) {
  if (occurrences == 0) {
    return count(element);
  }
  checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);

  AtomicInteger existingCounter = Maps.safeGet(countMap, element);
  if (existingCounter == null) {
    return 0;
  }
  while (true) {
    int oldValue = existingCounter.get();
    if (oldValue != 0) {
      int newValue = Math.max(0, oldValue - occurrences);
      if (existingCounter.compareAndSet(oldValue, newValue)) {
        if (newValue == 0) {
          // Just CASed to 0; remove the entry to clean up the map. If the removal fails,
          // another thread has already replaced it with a new counter, which is fine.
          countMap.remove(element, existingCounter);
        }
        return oldValue;
      }
    } else {
      return 0;
    }
  }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:46,代码来源:ConcurrentHashMultiset.java

示例2: removeExactly

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Removes exactly the specified number of occurrences of {@code element}, or makes no
 * change if this is not possible.
 *
 * <p>This method, in contrast to {@link #remove(Object, int)}, has no effect when the
 * element count is smaller than {@code occurrences}.
 *
 * @param element the element to remove
 * @param occurrences the number of occurrences of {@code element} to remove
 * @return {@code true} if the removal was possible (including if {@code occurrences} is zero)
 */
public boolean removeExactly(@Nullable Object element, int occurrences) {
  if (occurrences == 0) {
    return true;
  }
  checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);

  AtomicInteger existingCounter = Maps.safeGet(countMap, element);
  if (existingCounter == null) {
    return false;
  }
  while (true) {
    int oldValue = existingCounter.get();
    if (oldValue < occurrences) {
      return false;
    }
    int newValue = oldValue - occurrences;
    if (existingCounter.compareAndSet(oldValue, newValue)) {
      if (newValue == 0) {
        // Just CASed to 0; remove the entry to clean up the map. If the removal fails,
        // another thread has already replaced it with a new counter, which is fine.
        countMap.remove(element, existingCounter);
      }
      return true;
    }
  }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:38,代码来源:ConcurrentHashMultiset.java

示例3: remove

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Removes a number of occurrences of the specified element from this multiset. If the multiset
 * contains fewer than this number of occurrences to begin with, all occurrences will be removed.
 *
 * @param element the element whose occurrences should be removed
 * @param occurrences the number of occurrences of the element to remove
 * @return the count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative
 */
/*
 * TODO(cpovirk): remove and removeExactly currently accept null inputs only
 * if occurrences == 0. This satisfies both NullPointerTester and
 * CollectionRemoveTester.testRemove_nullAllowed, but it's not clear that it's
 * a good policy, especially because, in order for the test to pass, the
 * parameter must be misleadingly annotated as @Nullable. I suspect that
 * we'll want to remove @Nullable, add an eager checkNotNull, and loosen up
 * testRemove_nullAllowed.
 */
@CanIgnoreReturnValue
@Override
public int remove(@Nullable Object element, int occurrences) {
  if (occurrences == 0) {
    return count(element);
  }
  CollectPreconditions.checkPositive(occurrences, "occurences");

  AtomicInteger existingCounter = Maps.safeGet(countMap, element);
  if (existingCounter == null) {
    return 0;
  }
  while (true) {
    int oldValue = existingCounter.get();
    if (oldValue != 0) {
      int newValue = Math.max(0, oldValue - occurrences);
      if (existingCounter.compareAndSet(oldValue, newValue)) {
        if (newValue == 0) {
          // Just CASed to 0; remove the entry to clean up the map. If the removal fails,
          // another thread has already replaced it with a new counter, which is fine.
          countMap.remove(element, existingCounter);
        }
        return oldValue;
      }
    } else {
      return 0;
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:48,代码来源:ConcurrentHashMultiset.java

示例4: removeExactly

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Removes exactly the specified number of occurrences of {@code element}, or makes no
 * change if this is not possible.
 *
 * <p>This method, in contrast to {@link #remove(Object, int)}, has no effect when the
 * element count is smaller than {@code occurrences}.
 *
 * @param element the element to remove
 * @param occurrences the number of occurrences of {@code element} to remove
 * @return {@code true} if the removal was possible (including if {@code occurrences} is zero)
 * @throws IllegalArgumentException if {@code occurrences} is negative
 */
@CanIgnoreReturnValue
public boolean removeExactly(@Nullable Object element, int occurrences) {
  if (occurrences == 0) {
    return true;
  }
  CollectPreconditions.checkPositive(occurrences, "occurences");

  AtomicInteger existingCounter = Maps.safeGet(countMap, element);
  if (existingCounter == null) {
    return false;
  }
  while (true) {
    int oldValue = existingCounter.get();
    if (oldValue < occurrences) {
      return false;
    }
    int newValue = oldValue - occurrences;
    if (existingCounter.compareAndSet(oldValue, newValue)) {
      if (newValue == 0) {
        // Just CASed to 0; remove the entry to clean up the map. If the removal fails,
        // another thread has already replaced it with a new counter, which is fine.
        countMap.remove(element, existingCounter);
      }
      return true;
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:40,代码来源:ConcurrentHashMultiset.java

示例5: onNext

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Emits the given value if possible and terminates if there was an onComplete or onError
 * while emitting, drops the value otherwise.
 * @param <T> the value type
 * @param observer the target Observer to emit to
 * @param value the value to emit
 * @param wip the serialization work-in-progress counter/indicator
 * @param error the holder of Throwables
 */
public static <T> void onNext(Observer<? super T> observer, T value,
        AtomicInteger wip, AtomicThrowable error) {
    if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
        observer.onNext(value);
        if (wip.decrementAndGet() != 0) {
            Throwable ex = error.terminate();
            if (ex != null) {
                observer.onError(ex);
            } else {
                observer.onComplete();
            }
        }
    }
}
 
开发者ID:akarnokd,项目名称:RxJava3-preview,代码行数:24,代码来源:HalfSerializer.java

示例6: testCompareAndSetInMultipleThreads

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicInteger ai = new AtomicInteger(1);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(2, 3))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(1, 2));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertEquals(3, ai.get());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:AtomicIntegerTest.java

示例7: add

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Adds a number of occurrences of the specified element to this multiset.
 *
 * @param element the element to add
 * @param occurrences the number of occurrences to add
 * @return the previous count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative, or if
 *     the resulting amount would exceed {@link Integer#MAX_VALUE}
 */
@Override public int add(E element, int occurrences) {
  checkNotNull(element);
  if (occurrences == 0) {
    return count(element);
  }
  checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);

  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
      if (existingCounter == null) {
        return 0;
      }
      // existingCounter != null: fall through to operate against the existing AtomicInteger
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue != 0) {
        try {
          int newValue = IntMath.checkedAdd(oldValue, occurrences);
          if (existingCounter.compareAndSet(oldValue, newValue)) {
            // newValue can't == 0, so no need to check & remove
            return oldValue;
          }
        } catch (ArithmeticException overflow) {
          throw new IllegalArgumentException("Overflow adding " + occurrences
              + " occurrences to a count of " + oldValue);
        }
      } else {
        // In the case of a concurrent remove, we might observe a zero value, which means another
        // thread is about to remove (element, existingCounter) from the map. Rather than wait,
        // we can just do that work here.
        AtomicInteger newCounter = new AtomicInteger(occurrences);
        if ((countMap.putIfAbsent(element, newCounter) == null)
            || countMap.replace(element, existingCounter, newCounter)) {
          return 0;
        }
        break;
      }
    }

    // If we're still here, there was a race, so just try again.
  }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:56,代码来源:ConcurrentHashMultiset.java

示例8: setCount

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Adds or removes occurrences of {@code element} such that the {@link #count} of the
 * element becomes {@code count}.
 *
 * @return the count of {@code element} in the multiset before this call
 * @throws IllegalArgumentException if {@code count} is negative
 */
@Override public int setCount(E element, int count) {
  checkNotNull(element);
  checkNonnegative(count, "count");
  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      if (count == 0) {
        return 0;
      } else {
        existingCounter = countMap.putIfAbsent(element, new AtomicInteger(count));
        if (existingCounter == null) {
          return 0;
        }
        // existingCounter != null: fall through
      }
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue == 0) {
        if (count == 0) {
          return 0;
        } else {
          AtomicInteger newCounter = new AtomicInteger(count);
          if ((countMap.putIfAbsent(element, newCounter) == null)
              || countMap.replace(element, existingCounter, newCounter)) {
            return 0;
          }
        }
        break;
      } else {
        if (existingCounter.compareAndSet(oldValue, count)) {
          if (count == 0) {
            // Just CASed to 0; remove the entry to clean up the map. If the removal fails,
            // another thread has already replaced it with a new counter, which is fine.
            countMap.remove(element, existingCounter);
          }
          return oldValue;
        }
      }
    }
  }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:51,代码来源:ConcurrentHashMultiset.java

示例9: add

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Adds a number of occurrences of the specified element to this multiset.
 *
 * @param element the element to add
 * @param occurrences the number of occurrences to add
 * @return the previous count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative, or if
 *     the resulting amount would exceed {@link Integer#MAX_VALUE}
 */
@CanIgnoreReturnValue
@Override
public int add(E element, int occurrences) {
  checkNotNull(element);
  if (occurrences == 0) {
    return count(element);
  }
  CollectPreconditions.checkPositive(occurrences, "occurences");

  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
      if (existingCounter == null) {
        return 0;
      }
      // existingCounter != null: fall through to operate against the existing AtomicInteger
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue != 0) {
        try {
          int newValue = IntMath.checkedAdd(oldValue, occurrences);
          if (existingCounter.compareAndSet(oldValue, newValue)) {
            // newValue can't == 0, so no need to check & remove
            return oldValue;
          }
        } catch (ArithmeticException overflow) {
          throw new IllegalArgumentException(
              "Overflow adding " + occurrences + " occurrences to a count of " + oldValue);
        }
      } else {
        // In the case of a concurrent remove, we might observe a zero value, which means another
        // thread is about to remove (element, existingCounter) from the map. Rather than wait,
        // we can just do that work here.
        AtomicInteger newCounter = new AtomicInteger(occurrences);
        if ((countMap.putIfAbsent(element, newCounter) == null)
            || countMap.replace(element, existingCounter, newCounter)) {
          return 0;
        }
        break;
      }
    }

    // If we're still here, there was a race, so just try again.
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:58,代码来源:ConcurrentHashMultiset.java

示例10: setCount

import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
 * Adds or removes occurrences of {@code element} such that the {@link #count} of the
 * element becomes {@code count}.
 *
 * @return the count of {@code element} in the multiset before this call
 * @throws IllegalArgumentException if {@code count} is negative
 */
@CanIgnoreReturnValue
@Override
public int setCount(E element, int count) {
  checkNotNull(element);
  checkNonnegative(count, "count");
  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      if (count == 0) {
        return 0;
      } else {
        existingCounter = countMap.putIfAbsent(element, new AtomicInteger(count));
        if (existingCounter == null) {
          return 0;
        }
        // existingCounter != null: fall through
      }
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue == 0) {
        if (count == 0) {
          return 0;
        } else {
          AtomicInteger newCounter = new AtomicInteger(count);
          if ((countMap.putIfAbsent(element, newCounter) == null)
              || countMap.replace(element, existingCounter, newCounter)) {
            return 0;
          }
        }
        break;
      } else {
        if (existingCounter.compareAndSet(oldValue, count)) {
          if (count == 0) {
            // Just CASed to 0; remove the entry to clean up the map. If the removal fails,
            // another thread has already replaced it with a new counter, which is fine.
            countMap.remove(element, existingCounter);
          }
          return oldValue;
        }
      }
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:53,代码来源:ConcurrentHashMultiset.java


注:本文中的java.util.concurrent.atomic.AtomicInteger.compareAndSet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。