sun.misc.Unsafe.getObjectVolatile()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(316)

本文整理了Java中sun.misc.Unsafe.getObjectVolatile()方法的一些代码示例,展示了Unsafe.getObjectVolatile()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Unsafe.getObjectVolatile()方法的具体详情如下:
包路径:sun.misc.Unsafe
类名称:Unsafe
方法名:getObjectVolatile

Unsafe.getObjectVolatile介绍

[英]Gets an Object field from the given object, using volatile semantics.
[中]使用volatile语义从给定对象获取Object字段。

代码示例

代码示例来源:origin: h2oai/h2o-2

/**
 * Polls the given task only if it is at the current base.
 */
final boolean pollFor(ForkJoinTask<?> task) {
  ForkJoinTask<?>[] a; int b;
  if ((b = base) - top < 0 && (a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    if (U.getObjectVolatile(a, j) == task && base == b &&
      U.compareAndSwapObject(a, j, task, null)) {
      base = b + 1;
      return true;
    }
  }
  return false;
}

代码示例来源:origin: h2oai/h2o-2

/**
 * Takes a task in FIFO order if b is base of queue and a task
 * can be claimed without contention. Specialized versions
 * appear in ForkJoinPool methods scan and tryHelpStealer.
 */
final ForkJoinTask<?> pollAt(int b) {
  ForkJoinTask<?> t; ForkJoinTask<?>[] a;
  if ((a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null &&
      base == b &&
      U.compareAndSwapObject(a, j, t, null)) {
      base = b + 1;
      return t;
    }
  }
  return null;
}

代码示例来源:origin: robovm/robovm

/**
 * Takes a task in FIFO order if b is base of queue and a task
 * can be claimed without contention. Specialized versions
 * appear in ForkJoinPool methods scan and tryHelpStealer.
 */
final ForkJoinTask<?> pollAt(int b) {
  ForkJoinTask<?> t; ForkJoinTask<?>[] a;
  if ((a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null &&
      base == b &&
      U.compareAndSwapObject(a, j, t, null)) {
      base = b + 1;
      return t;
    }
  }
  return null;
}

代码示例来源:origin: com.typesafe.akka/akka-actor_2.12

/**
 * Takes a task in FIFO order if b is base of queue and a task
 * can be claimed without contention. Specialized versions
 * appear in ForkJoinPool methods scan and tryHelpStealer.
 */
final ForkJoinTask<?> pollAt(int b) {
  ForkJoinTask<?> t; ForkJoinTask<?>[] a;
  if ((a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null &&
      base == b &&
      U.compareAndSwapObject(a, j, t, null)) {
      base = b + 1;
      return t;
    }
  }
  return null;
}

代码示例来源:origin: com.typesafe.akka/akka-actor

/**
 * Takes a task in FIFO order if b is base of queue and a task
 * can be claimed without contention. Specialized versions
 * appear in ForkJoinPool methods scan and tryHelpStealer.
 */
final ForkJoinTask<?> pollAt(int b) {
  ForkJoinTask<?> t; ForkJoinTask<?>[] a;
  if ((a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null &&
      base == b &&
      U.compareAndSwapObject(a, j, t, null)) {
      base = b + 1;
      return t;
    }
  }
  return null;
}

代码示例来源:origin: com.typesafe.akka/akka-actor_2.11

/**
 * Takes a task in FIFO order if b is base of queue and a task
 * can be claimed without contention. Specialized versions
 * appear in ForkJoinPool methods scan and tryHelpStealer.
 */
final ForkJoinTask<?> pollAt(int b) {
  ForkJoinTask<?> t; ForkJoinTask<?>[] a;
  if ((a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null &&
      base == b &&
      U.compareAndSwapObject(a, j, t, null)) {
      base = b + 1;
      return t;
    }
  }
  return null;
}

代码示例来源:origin: h2oai/h2o-2

/**
 * Takes next task, if one exists, in FIFO order.
 */
final ForkJoinTask<?> poll() {
  ForkJoinTask<?>[] a; int b; ForkJoinTask<?> t;
  while ((b = base) - top < 0 && (a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    t = (ForkJoinTask<?>)U.getObjectVolatile(a, j);
    if (t != null) {
      if (base == b &&
        U.compareAndSwapObject(a, j, t, null)) {
        base = b + 1;
        return t;
      }
    }
    else if (base == b) {
      if (b + 1 == top)
        break;
      Thread.yield(); // wait for lagging update
    }
  }
  return null;
}

代码示例来源:origin: robovm/robovm

/**
 * Takes next task, if one exists, in FIFO order.
 */
final ForkJoinTask<?> poll() {
  ForkJoinTask<?>[] a; int b; ForkJoinTask<?> t;
  while ((b = base) - top < 0 && (a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    t = (ForkJoinTask<?>)U.getObjectVolatile(a, j);
    if (t != null) {
      if (base == b &&
        U.compareAndSwapObject(a, j, t, null)) {
        base = b + 1;
        return t;
      }
    }
    else if (base == b) {
      if (b + 1 == top)
        break;
      Thread.yield(); // wait for lagging update (very rare)
    }
  }
  return null;
}

代码示例来源:origin: com.typesafe.akka/akka-actor

/**
 * Takes next task, if one exists, in FIFO order.
 */
final ForkJoinTask<?> poll() {
  ForkJoinTask<?>[] a; int b; ForkJoinTask<?> t;
  while ((b = base) - top < 0 && (a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    t = (ForkJoinTask<?>)U.getObjectVolatile(a, j);
    if (t != null) {
      if (base == b &&
        U.compareAndSwapObject(a, j, t, null)) {
        base = b + 1;
        return t;
      }
    }
    else if (base == b) {
      if (b + 1 == top)
        break;
      Thread.yield(); // wait for lagging update (very rare)
    }
  }
  return null;
}

代码示例来源:origin: com.typesafe.akka/akka-actor_2.11

/**
 * Takes next task, if one exists, in FIFO order.
 */
final ForkJoinTask<?> poll() {
  ForkJoinTask<?>[] a; int b; ForkJoinTask<?> t;
  while ((b = base) - top < 0 && (a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    t = (ForkJoinTask<?>)U.getObjectVolatile(a, j);
    if (t != null) {
      if (base == b &&
        U.compareAndSwapObject(a, j, t, null)) {
        base = b + 1;
        return t;
      }
    }
    else if (base == b) {
      if (b + 1 == top)
        break;
      Thread.yield(); // wait for lagging update (very rare)
    }
  }
  return null;
}

代码示例来源:origin: com.typesafe.akka/akka-actor_2.12

/**
 * Takes next task, if one exists, in FIFO order.
 */
final ForkJoinTask<?> poll() {
  ForkJoinTask<?>[] a; int b; ForkJoinTask<?> t;
  while ((b = base) - top < 0 && (a = array) != null) {
    int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
    t = (ForkJoinTask<?>)U.getObjectVolatile(a, j);
    if (t != null) {
      if (base == b &&
        U.compareAndSwapObject(a, j, t, null)) {
        base = b + 1;
        return t;
      }
    }
    else if (base == b) {
      if (b + 1 == top)
        break;
      Thread.yield(); // wait for lagging update (very rare)
    }
  }
  return null;
}

代码示例来源:origin: robovm/robovm

/**
 * Returns the segment for the given index, creating it and
 * recording in segment table (via CAS) if not already present.
 *
 * @param k the index
 * @return the segment
 */
@SuppressWarnings("unchecked")
private Segment<K,V> ensureSegment(int k) {
  final Segment<K,V>[] ss = this.segments;
  long u = (k << SSHIFT) + SBASE; // raw offset
  Segment<K,V> seg;
  if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) == null) {
    Segment<K,V> proto = ss[0]; // use segment 0 as prototype
    int cap = proto.table.length;
    float lf = proto.loadFactor;
    int threshold = (int)(cap * lf);
    HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry<?,?>[cap];
    if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
      == null) { // recheck
      Segment<K,V> s = new Segment<K,V>(lf, threshold, tab);
      while ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
          == null) {
        if (UNSAFE.compareAndSwapObject(ss, u, null, seg = s))
          break;
      }
    }
  }
  return seg;
}

代码示例来源:origin: robovm/robovm

/**
 * Initializes or doubles the capacity of array. Call either
 * by owner or with lock held -- it is OK for base, but not
 * top, to move while resizings are in progress.
 */
final ForkJoinTask<?>[] growArray() {
  ForkJoinTask<?>[] oldA = array;
  int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY;
  if (size > MAXIMUM_QUEUE_CAPACITY)
    throw new RejectedExecutionException("Queue capacity exceeded");
  int oldMask, t, b;
  ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size];
  if (oldA != null && (oldMask = oldA.length - 1) >= 0 &&
    (t = top) - (b = base) > 0) {
    int mask = size - 1;
    do {
      ForkJoinTask<?> x;
      int oldj = ((b & oldMask) << ASHIFT) + ABASE;
      int j    = ((b &    mask) << ASHIFT) + ABASE;
      x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
      if (x != null &&
        U.compareAndSwapObject(oldA, oldj, x, null))
        U.putObjectVolatile(a, j, x);
    } while (++b != t);
  }
  return a;
}

代码示例来源:origin: com.typesafe.akka/akka-actor_2.12

/**
 * Initializes or doubles the capacity of array. Call either
 * by owner or with lock held -- it is OK for base, but not
 * top, to move while resizings are in progress.
 */
final ForkJoinTask<?>[] growArray() {
  ForkJoinTask<?>[] oldA = array;
  int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY;
  if (size > MAXIMUM_QUEUE_CAPACITY)
    throw new RejectedExecutionException("Queue capacity exceeded");
  int oldMask, t, b;
  ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size];
  if (oldA != null && (oldMask = oldA.length - 1) >= 0 &&
    (t = top) - (b = base) > 0) {
    int mask = size - 1;
    do {
      ForkJoinTask<?> x;
      int oldj = ((b & oldMask) << ASHIFT) + ABASE;
      int j    = ((b &    mask) << ASHIFT) + ABASE;
      x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
      if (x != null &&
        U.compareAndSwapObject(oldA, oldj, x, null))
        U.putObjectVolatile(a, j, x);
    } while (++b != t);
  }
  return a;
}

代码示例来源:origin: com.typesafe.akka/akka-actor_2.11

/**
 * Initializes or doubles the capacity of array. Call either
 * by owner or with lock held -- it is OK for base, but not
 * top, to move while resizings are in progress.
 */
final ForkJoinTask<?>[] growArray() {
  ForkJoinTask<?>[] oldA = array;
  int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY;
  if (size > MAXIMUM_QUEUE_CAPACITY)
    throw new RejectedExecutionException("Queue capacity exceeded");
  int oldMask, t, b;
  ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size];
  if (oldA != null && (oldMask = oldA.length - 1) >= 0 &&
    (t = top) - (b = base) > 0) {
    int mask = size - 1;
    do {
      ForkJoinTask<?> x;
      int oldj = ((b & oldMask) << ASHIFT) + ABASE;
      int j    = ((b &    mask) << ASHIFT) + ABASE;
      x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
      if (x != null &&
        U.compareAndSwapObject(oldA, oldj, x, null))
        U.putObjectVolatile(a, j, x);
    } while (++b != t);
  }
  return a;
}

代码示例来源:origin: h2oai/h2o-2

int oldj = ((b & oldMask) << ASHIFT) + ABASE;
  int j    = ((b &    mask) << ASHIFT) + ABASE;
  x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
  if (x != null &&
    U.compareAndSwapObject(oldA, oldj, x, null))
    U.putObjectVolatile(a, j, x);
} while (++b != t);

代码示例来源:origin: com.typesafe.akka/akka-actor

/**
 * Initializes or doubles the capacity of array. Call either
 * by owner or with lock held -- it is OK for base, but not
 * top, to move while resizings are in progress.
 */
final ForkJoinTask<?>[] growArray() {
  ForkJoinTask<?>[] oldA = array;
  int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY;
  if (size > MAXIMUM_QUEUE_CAPACITY)
    throw new RejectedExecutionException("Queue capacity exceeded");
  int oldMask, t, b;
  ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size];
  if (oldA != null && (oldMask = oldA.length - 1) >= 0 &&
    (t = top) - (b = base) > 0) {
    int mask = size - 1;
    do {
      ForkJoinTask<?> x;
      int oldj = ((b & oldMask) << ASHIFT) + ABASE;
      int j    = ((b &    mask) << ASHIFT) + ABASE;
      x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
      if (x != null &&
        U.compareAndSwapObject(oldA, oldj, x, null))
        U.putObjectVolatile(a, j, x);
    } while (++b != t);
  }
  return a;
}

代码示例来源:origin: h2oai/h2o-2

int i = (((a.length - 1) & b) << ASHIFT) + ABASE;
ForkJoinTask<?> t =
  (ForkJoinTask<?>)U.getObjectVolatile(a, i);
if (subtask.status < 0 || j.currentJoin != subtask ||
  v.currentSteal != subtask)
stat = 1;               // apparent progress
if (t != null && v.base == b &&
  U.compareAndSwapObject(a, i, t, null)) {
  v.base = b + 1;     // help stealer
  joiner.runSubtask(t);

代码示例来源:origin: robovm/robovm

int i = (((a.length - 1) & b) << ASHIFT) + ABASE;
ForkJoinTask<?> t =
  (ForkJoinTask<?>)U.getObjectVolatile(a, i);
if (subtask.status < 0 || j.currentJoin != subtask ||
  v.currentSteal != subtask)
stat = 1;               // apparent progress
if (t != null && v.base == b &&
  U.compareAndSwapObject(a, i, t, null)) {
  v.base = b + 1;     // help stealer
  joiner.runSubtask(t);

代码示例来源:origin: h2oai/h2o-2

for (ForkJoinTask<?> t;;) {           // traverse from s to b
  int j = ((--s & m) << ASHIFT) + ABASE;
  t = (ForkJoinTask<?>)U.getObjectVolatile(a, j);
  if (t == null)                    // inconsistent length
    break;
  else if (t == task) {
    if (s + 1 == top) {           // pop
      if (!U.compareAndSwapObject(a, j, task, null))
        break;
      top = s;
      removed = U.compareAndSwapObject(a, j, task,
                       new EmptyTask());
    break;
    empty = false;
  else if (s + 1 == top) {          // pop and throw away
    if (U.compareAndSwapObject(a, j, t, null))
      top = s;
    break;

相关文章

Unsafe类方法