-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhangling
committed
May 21, 2022
1 parent
7ef56ae
commit f5919ed
Showing
10 changed files
with
479 additions
and
0 deletions.
There are no files selected for viewing
Binary file modified
BIN
-4.73 KB
(100%)
src/main/java/com/zl/javabingfabianchengshizhan/Java并发编程实战.pdf
Binary file not shown.
28 changes: 28 additions & 0 deletions
28
src/main/java/com/zl/javabingfabianchengshizhan/thread15/AtomicPseudoRandom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/zl/javabingfabianchengshizhan/thread15/CASCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/com/zl/javabingfabianchengshizhan/thread15/CasNumberRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
|
51 changes: 51 additions & 0 deletions
51
src/main/java/com/zl/javabingfabianchengshizhan/thread15/ConcurrentStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/zl/javabingfabianchengshizhan/thread15/CurrLinkedQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/zl/javabingfabianchengshizhan/thread15/LinkedQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.