Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.62 KB | None | 0 0
  1. import java.util.concurrent.atomic.AtomicInteger;
  2. import static java.lang.Float.*;
  3.  
  4. class AtomicFloat extends Number {
  5.  
  6. private AtomicInteger bits;
  7.  
  8. public AtomicFloat() {
  9. this(0f);
  10. }
  11.  
  12. public AtomicFloat(float initialValue) {
  13. bits = new AtomicInteger(floatToIntBits(initialValue));
  14. }
  15.  
  16. public final boolean compareAndSet(float expect, float update) {
  17. return bits.compareAndSet(floatToIntBits(expect),
  18. floatToIntBits(update));
  19. }
  20.  
  21. public final void set(float newValue) {
  22. bits.set(floatToIntBits(newValue));
  23. }
  24.  
  25. public final float get() {
  26. return intBitsToFloat(bits.get());
  27. }
  28.  
  29. public float floatValue() {
  30. return get();
  31. }
  32.  
  33. public final float getAndSet(float newValue) {
  34. return intBitsToFloat(bits.getAndSet(floatToIntBits(newValue)));
  35. }
  36.  
  37. public final boolean weakCompareAndSet(float expect, float update) {
  38. return bits.weakCompareAndSet(floatToIntBits(expect),
  39. floatToIntBits(update));
  40. }
  41.  
  42. public double doubleValue() { return (double) floatValue(); }
  43. public int intValue() { return (int) get(); }
  44. public long longValue() { return (long) get(); }
  45.  
  46. }
  47.  
  48. class AtomicDouble {
  49. private val value = new AtomicReference(java.lang.Double.valueOf(0.0))
  50. @tailrec
  51. final def getAndAdd(delta: Double): Double = {
  52. val currentValue = value.get
  53. val newValue = java.lang.Double.valueOf(currentValue.doubleValue + delta)
  54. if (value.compareAndSet(currentValue, newValue))
  55. currentValue.doubleValue
  56. else
  57. getAndAdd(delta) // try, try again
  58. }
  59. }
  60.  
  61. class AtomicDouble {
  62. private AtomicReference<Double> value = new AtomicReference(Double.valueOf(0.0));
  63. double getAndAdd(double delta) {
  64. while (true) {
  65. Double currentValue = value.get();
  66. Double newValue = Double.valueOf(currentValue.doubleValue() + delta);
  67. if (value.compareAndSet(currentValue, newValue))
  68. return currentValue.doubleValue();
  69. }
  70. }
  71. }
  72.  
  73. average.set( average.get() > x ? dosomething(y) : y) ;
  74.  
  75. average.updateAndGet(new DoubleUnaryOperator() {
  76. @Override
  77. public double applyAsDouble( double previous ) {
  78. return previous > x ? dosomething(y) : y;
  79. }
  80. });
  81.  
  82. import static java.lang.Double.doubleToLongBits;
  83. import static java.lang.Double.longBitsToDouble;
  84.  
  85. import java.util.concurrent.atomic.AtomicLong;
  86. import java.util.function.DoubleBinaryOperator;
  87. import java.util.function.DoubleUnaryOperator;
  88.  
  89. public final class AtomicDouble extends Number {
  90. private static final long serialVersionUID = 12327722191124184L;
  91.  
  92. private final AtomicLong bits;
  93.  
  94. public AtomicDouble() {
  95. this(0.0d);
  96. }
  97.  
  98. public AtomicDouble( double initialValue ) {
  99. bits = new AtomicLong( toLong(initialValue) );
  100. }
  101.  
  102. /**
  103. * Atomically sets the value to the given updated value
  104. * if the current value {@code ==} the expected value.
  105. *
  106. * @param expect the expected value
  107. * @param update the new value
  108. * @return {@code true} if successful. False return indicates that
  109. * the actual value was not equal to the expected value.
  110. */
  111. public final boolean compareAndSet( double expect, double update ) {
  112. return bits.compareAndSet(toLong(expect), toLong(update));
  113. }
  114.  
  115. /**
  116. * Sets to the given value.
  117. *
  118. * @param newValue the new value
  119. */
  120. public final void set( double newValue ) {
  121. bits.set(toLong(newValue));
  122. }
  123.  
  124. public final double get() {
  125. return toDouble(bits.get());
  126. }
  127.  
  128. /**
  129. * Atomically sets to the given value and returns the old value.
  130. *
  131. * @param newValue the new value
  132. * @return the previous value
  133. */
  134. public final double getAndSet( double newValue ) {
  135. return toDouble( bits.getAndSet(toLong(newValue)) );
  136. }
  137.  
  138. /**
  139. * Atomically sets the value to the given updated value
  140. * if the current value {@code ==} the expected value.
  141. *
  142. * <p><a href="package-summary.html#weakCompareAndSet">May fail
  143. * spuriously and does not provide ordering guarantees</a>, so is
  144. * only rarely an appropriate alternative to {@code compareAndSet}.
  145. *
  146. * @param expect the expected value
  147. * @param update the new value
  148. * @return {@code true} if successful
  149. */
  150. public final boolean weakCompareAndSet( double expect, double update ) {
  151. return bits.weakCompareAndSet(toLong(expect), toLong(update));
  152. }
  153.  
  154. /**
  155. * Atomically updates the current value with the results of
  156. * applying the given function to the current and given values,
  157. * returning the updated value. The function should be
  158. * side-effect-free, since it may be re-applied when attempted
  159. * updates fail due to contention among threads. The function
  160. * is applied with the current value as its first argument,
  161. * and the given update as the second argument.
  162. *
  163. * @param x the update value
  164. * @param accumulatorFunction a side-effect-free function of two arguments
  165. * @return the updated value
  166. * @since 1.8
  167. */
  168. public final double accumulateAndGet( double x, DoubleBinaryOperator accumulatorFunction ) {
  169. double prev, next;
  170. do {
  171. prev = get();
  172. next = accumulatorFunction.applyAsDouble(prev, x);
  173. } while (!compareAndSet(prev, next));
  174. return next;
  175. }
  176.  
  177. /**
  178. * Atomically adds the given value to the current value.
  179. *
  180. * @param delta the value to add
  181. * @return the updated value
  182. */
  183. public final double addAndGet( double delta ) {
  184. return toDouble(bits.addAndGet(toLong(delta)));
  185. }
  186.  
  187. /**
  188. * Atomically decrements by one the current value.
  189. *
  190. * @return the updated value
  191. */
  192. public final double decrementAndGet() {
  193. return addAndGet(-1.0d);
  194. }
  195.  
  196. /**
  197. * Atomically updates the current value with the results of
  198. * applying the given function to the current and given values,
  199. * returning the previous value. The function should be
  200. * side-effect-free, since it may be re-applied when attempted
  201. * updates fail due to contention among threads. The function
  202. * is applied with the current value as its first argument,
  203. * and the given update as the second argument.
  204. *
  205. * @param x the update value
  206. * @param accumulatorFunction a side-effect-free function of two arguments
  207. * @return the previous value
  208. * @since 1.8
  209. */
  210. public final double getAndAccumulate( double x, DoubleBinaryOperator accumulatorFunction ) {
  211. double prev, next;
  212. do {
  213. prev = get();
  214. next = accumulatorFunction.applyAsDouble(prev, x);
  215. } while (!compareAndSet(prev, next));
  216. return prev;
  217. }
  218.  
  219. /**
  220. * Atomically adds the given value to the current value.
  221. *
  222. * @param delta the value to add
  223. * @return the previous value
  224. */
  225. public final double getAndAdd( double delta ) {
  226. return toDouble(bits.getAndAdd(toLong(delta)));
  227. }
  228.  
  229. public final double getAndDecrement() {
  230. return getAndAdd(-1.0d);
  231. }
  232.  
  233. /**
  234. * Atomically increments by one the current value.
  235. *
  236. * @return the previous value
  237. */
  238. public final double getAndIncrement() {
  239. return getAndAdd(1.0d);
  240. }
  241.  
  242. /**
  243. * Atomically increments by one the current value.
  244. *
  245. * @return the updated value
  246. */
  247. public final double incrementAndGet() {
  248. return addAndGet(1.0d);
  249. }
  250.  
  251. /**
  252. * Atomically updates the current value with the results of
  253. * applying the given function, returning the previous value. The
  254. * function should be side-effect-free, since it may be re-applied
  255. * when attempted updates fail due to contention among threads.
  256. *
  257. * @param updateFunction a side-effect-free function
  258. * @return the previous value
  259. * @since 1.8
  260. */
  261. public final double getAndUpdate( DoubleUnaryOperator updateFunction ) {
  262. double prev, next;
  263. do {
  264. prev = get();
  265. next = updateFunction.applyAsDouble(prev);
  266. } while (!compareAndSet(prev, next));
  267. return prev;
  268. }
  269.  
  270.  
  271. /**
  272. * Eventually sets to the given value.
  273. *
  274. * @param newValue the new value
  275. * @since 1.6
  276. */
  277. public final void lazySet( double newValue ) {
  278. bits.lazySet(toLong(newValue));
  279. // unsafe.putOrderedLong(this, valueOffset, newValue);
  280. }
  281.  
  282. /**
  283. * Returns the value of this {@code AtomicLong} as a {@code long}.
  284. */
  285. public long longValue() {
  286. return (long) get();
  287. }
  288.  
  289. /**
  290. * Returns the String representation of the current value.
  291. *
  292. * @return the String representation of the current value
  293. */
  294. public String toString() {
  295. return Double.toString(get());
  296. }
  297.  
  298. /**
  299. * Atomically updates the current value with the results of
  300. * applying the given function, returning the updated value. The
  301. * function should be side-effect-free, since it may be re-applied
  302. * when attempted updates fail due to contention among threads.
  303. *
  304. * @param updateFunction a side-effect-free function
  305. * @return the updated value
  306. * @since 1.8
  307. */
  308. public final double updateAndGet( DoubleUnaryOperator updateFunction ) {
  309. double prev, next;
  310. do {
  311. prev = get();
  312. next = updateFunction.applyAsDouble(prev);
  313. } while (!compareAndSet(prev, next));
  314. return next;
  315. }
  316. /**
  317. * Returns the value of this {@code AtomicLong} as an {@code int}
  318. * after a narrowing primitive conversion.
  319. *
  320. * @jls 5.1.3 Narrowing Primitive Conversions
  321. */
  322. public int intValue() {
  323. return (int) get();
  324. }
  325.  
  326. /**
  327. * Returns the value of this {@code AtomicLong} as a {@code float}
  328. * after a widening primitive conversion.
  329. *
  330. * @jls 5.1.2 Widening Primitive Conversions
  331. */
  332. public float floatValue() {
  333. return (float) get();
  334. }
  335.  
  336. /**
  337. * Returns the value of this {@code AtomicLong} as a {@code double}
  338. * after a widening primitive conversion.
  339. *
  340. * @jls 5.1.2 Widening Primitive Conversions
  341. */
  342. public double doubleValue() {
  343. return get();
  344. }
  345.  
  346. private static double toDouble( long l ) {
  347. return longBitsToDouble(l);
  348. }
  349.  
  350. private static long toLong( double delta ) {
  351. return doubleToLongBits(delta);
  352. }
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement