Skip to content

Commit

Permalink
add:并发编程实战代码示例15章
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangling committed May 21, 2022
1 parent 7ef56ae commit f5919ed
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 0 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.zl.javabingfabianchengshizhan.thread15;

import java.util.concurrent.atomic.AtomicInteger;

/**
* @author Allen.zhang
* @title: AtomicPseudoRandom
* @projectName zl
* @description: TODO
* @date 2022/5/1215:32
*/
public class AtomicPseudoRandom extends PseudoRandom{
private AtomicInteger seed;
AtomicPseudoRandom(int seed) {
this.seed = new AtomicInteger(seed);
}

public int nextInt(int n) {
while (true) {
int s = seed.get();
int nextSeed = calculateNext(s);
if (seed.compareAndSet(s, nextSeed)){
int remainder = s%n;
return remainder>0?remainder: remainder+n;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.zl.javabingfabianchengshizhan.thread15;

import net.jcip.annotations.ThreadSafe;

/**
* @author Allen.zhang
* @title: CASCounter
* @projectName zl
* @description: TODO
* @date 2022/5/1021:14
*/
@ThreadSafe
public class CASCounter {
private SimulatedCAS value;

public int getValue() {
return value.get();
}

public int increment() {
int v;
do {
v = value.get();
}while (v != value.compareAndSwap(v, v+1));
return v+1;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.zl.javabingfabianchengshizhan.thread15;

import net.jcip.annotations.Immutable;
import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.atomic.AtomicReference;

/**
* @author Allen.zhang
* @title: CasNumberRange
* @projectName zl
* @description: TODO
* @date 2022/5/1112:24
*/
@ThreadSafe
public class CasNumberRange {

@Immutable
private static class IntPair {
final int lower;
final int upper;

public IntPair(int lower, int upper) {
this.lower = lower;
this.upper = upper;
}
}

private final AtomicReference<IntPair> values =
new AtomicReference<>(new IntPair(0, 0));

public int getLower() {
return values.get().lower;
}

public int getUpper() {
return values.get().upper;
}

public void setLower(int i) {
while (true) {
IntPair oldv = values.get();
if (i > oldv.upper) {
throw new IllegalArgumentException("Can't set lower to " + i + " > upper");
}

IntPair newv = new IntPair(i, oldv.upper);
if (values.compareAndSet(oldv, newv))
return;
}

}

public void setUpper(int i) {

while (true) {
IntPair oldv = values.get();
if (i < oldv.lower) {
throw new IllegalArgumentException("Can't set lower to " + i + " < lower");
}

IntPair newv = new IntPair(oldv.lower, i);
if (values.compareAndSet(oldv, newv))
return;
}

}

public static void main(String[] args) {
CasNumberRange casNumberRange = new CasNumberRange();
System.out.println(casNumberRange.getLower());
System.out.println(casNumberRange.getUpper());

int[] arr = new int[]{1,4,6,8,4,28,2,0,4,39,11};
for (int i = 0; i < arr.length; i++) {
casNumberRange.setUpper(arr[i]);
}

System.out.println(casNumberRange.getUpper());
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.zl.javabingfabianchengshizhan.thread15;

import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.atomic.AtomicReference;

/**
* @author Allen.zhang
* @title: ConcurrentStack
* @projectName zl
* @description: TODO
* @date 2022/5/1821:55
*/
@ThreadSafe
public class ConcurrentStack <E> {
AtomicReference<Node<E>> top = new AtomicReference<>();


public E pop(){
Node<E> oldHead;
Node<E> newHead;
do {
oldHead = top.get();
if (oldHead == null)
return null;
newHead = oldHead.next;
}while (!top.compareAndSet(oldHead, newHead));

return oldHead.item;
}


public void push(E item){
Node<E> newHead = new Node<E>(item);
Node<E> oldHead;

do {
oldHead = top.get();
newHead.next = oldHead;
} while (!top.compareAndSet(oldHead, newHead));

}

private static class Node<E> {
public final E item;
public Node<E> next;
public Node(E item) {
this.item = item;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.zl.javabingfabianchengshizhan.thread15;

import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
* @author Allen.zhang
* @title: LinkedQueue
* @projectName zl
* @description: TODO
* @date 2022/5/210:45
*/
@ThreadSafe
public class CurrLinkedQueue<E> {

private static class Node<E> {
private E item;
private volatile Node<E> next;

public Node(E item) {
this.item = item;
}

private boolean compareAndSet(Node<E> expect, Node<E> update){
return updater.compareAndSet(this, expect, update);
}
}

private static AtomicReferenceFieldUpdater<Node, Node> updater =
AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "next");



private final Node<E> dummy = new Node<E>(null);
private final AtomicReference<Node<E>> head = new AtomicReference<Node<E>>(dummy);
private final AtomicReference<Node<E>> tail = new AtomicReference<>(dummy);

public boolean put(E item) {
Node<E> newNode = new Node(item);
while (true) {
Node<E> curTail = tail.get();
Node<E> tailNext = curTail.next;

if (curTail != tail.get()) {
continue;
}

if (tailNext != null) {
//队列处于中间状态,尝试推进尾节点
tail.compareAndSet(curTail, tailNext);
} else if (curTail.next.compareAndSet(null, newNode)) { //处于稳定状态,尝试推进新节点
tail.compareAndSet(curTail, newNode); //插入操作成功,尝试推进尾节点
return true;
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.zl.javabingfabianchengshizhan.thread15;

import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.atomic.AtomicReference;

/**
* @author Allen.zhang
* @title: LinkedQueue
* @projectName zl
* @description: TODO
* @date 2022/5/210:45
*/
@ThreadSafe
public class LinkedQueue <E> {

private static class Node<E> {
final E item;
final AtomicReference<Node<E>> next;

public Node(E item, Node<E> next) {
this.item = item;
this.next = new AtomicReference<>(next);
}
}

private final Node<E> dummy = new Node<E>(null, null);
private final AtomicReference<Node<E>> head = new AtomicReference<Node<E>>(dummy);
private final AtomicReference<Node<E>> tail = new AtomicReference<>(dummy);

public boolean put(E item) {
Node<E> newNode = new Node(item, null);
while (true) {
Node<E> curTail = tail.get();
Node<E> tailNext = curTail.next.get();

if (curTail != tail.get()) {
continue;
}

if (tailNext != null) {
//队列处于中间状态,尝试推进尾节点
tail.compareAndSet(curTail, tailNext);
} else if (curTail.next.compareAndSet(null, newNode)) { //处于稳定状态,尝试推进新节点
tail.compareAndSet(curTail, newNode); //插入操作成功,尝试推进尾节点
return true;
}
}
}

}
Loading

0 comments on commit f5919ed

Please sign in to comment.