public void dispose() { storedScript.weakCompareAndSet( expected, loadScript() ); } }
/** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> * and does not provide ordering guarantees, so is only rarely an * appropriate alternative to {@code compareAndSet}. * * @param expect the expected value * @param update the new value * @return true if successful. */ public boolean weakCompareAndSet(BigInteger expect, BigInteger update) { return valueHolder.weakCompareAndSet(expect, update); }
boolean setState(Set<TaskState> expected, TaskState newState) { TaskState current; do { current = state.get(); if(!expected.contains(current)) return false; } while(!state.weakCompareAndSet(current, newState)); return true; }
boolean setState(Set<CONNECTION_STATE> expected, CONNECTION_STATE newState) { CONNECTION_STATE current; do { current = state.get(); if(!expected.contains(current)) return false; } while(!state.weakCompareAndSet(current, newState)); if(metaHandler != null) metaHandler.onStateChange(current, newState); return true; }
private static final AtomicReference<Object> ref = new AtomicReference<>(); // <Object> as we require two distinguished values. :( // (I guess could use a mark.) public static Singleton getInstance() { for (;;) { // May want to play with park to avoid spinning. Object obj = ref.get(); if (obj instanceof Singleton) { return (Singleton)obj; } if (ref.weakCompareAndSet(null, Thread.currentThread())) { Singleton instance = null; // To reset on fail. try { instance = new Singleton(); } finally { ref.set(instance); } return instance; } } }
@Override public void apply(B value) { if (!ref.weakCompareAndSet(null, value)) { promise.set(new Pair((T) ref.get(), value)); } } });
@Override public void apply(T value) { if (!ref.weakCompareAndSet(null, value)) { promise.set(new Pair(value, (B) ref.get())); } } });
@Override Window rollup() { // Swap the queue (which should work, since we should be the only concurrent thread doing this) ... Queue<DurationActivity> durations = null; if (this.durations.weakCompareAndSet(duration1, duration2)) { durations = duration1; } else { this.durations.weakCompareAndSet(duration2, duration1); durations = duration2; } // Make a copy to minimize the time spent using the durations ... List<DurationActivity> records = new ArrayList<DurationActivity>(durations); durations.clear(); // Now add to the largest durations and compute the statistics ... int numRecords = records.size(); long[] values = new long[numRecords]; int i = 0; for (DurationActivity record : records) { values[i++] = record != null ? record.getDuration(TimeUnit.MILLISECONDS) : 0L; this.largestDurations.add(record); while (this.largestDurations.size() > this.retentionSize) { this.largestDurations.poll(); // remove the smallest duration from the front of the queue } } Statistics stats = statisticsFor(values); return recordStatisticsForLastSecond(stats); }
@Override Window rollup() { // Swap the queue (which should work, since we should be the only concurrent thread doing this) ... Queue<DurationActivity> durations = null; if (this.durations.weakCompareAndSet(duration1, duration2)) { durations = duration1; } else { this.durations.weakCompareAndSet(duration2, duration1); durations = duration2; } // Make a copy to minimize the time spent using the durations ... List<DurationActivity> records = new ArrayList<DurationActivity>(durations); durations.clear(); // Now add to the largest durations and compute the statistics ... int numRecords = records.size(); long[] values = new long[numRecords]; int i = 0; for (DurationActivity record : records) { values[i++] = record != null ? record.getDuration(TimeUnit.MILLISECONDS) : 0L; this.largestDurations.add(record); while (this.largestDurations.size() > this.retentionSize) { this.largestDurations.poll(); // remove the smallest duration from the front of the queue } } Statistics stats = statisticsFor(values); return recordStatisticsForLastSecond(stats); }