Skip to content

Commit 7b55910

Browse files
authored
Add files via upload
1 parent fa9c7a8 commit 7b55910

38 files changed

+71145
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.jd.thread.BlockingIO;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Future;
6+
7+
public class IoBoundApplication {
8+
private static final int NUMBER_OF_TASKS = 10_0000;
9+
10+
public static void main(String[] args) {
11+
System.out.printf("Running %d tasks\n", NUMBER_OF_TASKS);
12+
13+
long start = System.currentTimeMillis();
14+
performTasks();
15+
System.out.printf("Tasks took %dms to complete\n", System.currentTimeMillis() - start);
16+
}
17+
18+
private static void performTasks() {
19+
try (ExecutorService executorService = Executors.newCachedThreadPool()) {
20+
21+
for (int i = 0; i < NUMBER_OF_TASKS; i++) {
22+
executorService.submit(() -> blockingIoOperation());
23+
}
24+
}
25+
// ExecutorService executor = Executors.newFixedThreadPool(1000);
26+
//
27+
// for (int i = 0; i < NUMBER_OF_TASKS; i++) {
28+
// Future<?> submit = executor.submit(() -> blockingIoOperation());
29+
//
30+
// }
31+
// executor.shutdown();
32+
33+
}
34+
35+
// Simulates a long blocking IO
36+
private static void blockingIoOperation() {
37+
System.out.println("Executing a blocking task from thread: " + Thread.currentThread());
38+
try {
39+
Thread.sleep(1000);
40+
} catch (InterruptedException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.jd.thread.LockFree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
import java.util.concurrent.atomic.AtomicReference;
8+
import java.util.concurrent.locks.LockSupport;
9+
10+
public class AotmicReferenceEx {
11+
public static void main(String[] args) throws InterruptedException {
12+
StandardStack<Integer> stack = new StandardStack<>();
13+
// LockFreeStack<Integer> stack = new LockFreeStack<>();
14+
Random random = new Random();
15+
16+
for (int i = 0; i < 100000; i++) {
17+
stack.push(random.nextInt());
18+
}
19+
20+
List<Thread> threads = new ArrayList<>();
21+
22+
int pushingThreads = 2;
23+
int poppingThreads = 2;
24+
25+
for (int i = 0; i < pushingThreads; i++) {
26+
Thread thread = new Thread(() -> {
27+
while (true) {
28+
stack.push(random.nextInt());
29+
}
30+
});
31+
32+
thread.setDaemon(true);
33+
threads.add(thread);
34+
}
35+
36+
for (int i = 0; i < poppingThreads; i++) {
37+
Thread thread = new Thread(() -> {
38+
while (true) {
39+
stack.pop();
40+
}
41+
});
42+
43+
thread.setDaemon(true);
44+
threads.add(thread);
45+
}
46+
47+
for (Thread thread : threads) {
48+
thread.start();
49+
}
50+
51+
Thread.sleep(10000);
52+
53+
System.out.println(String.format("%,d operations were performed in 10 seconds ", stack.getCounter()));
54+
}
55+
56+
public static class LockFreeStack<T> {
57+
private AtomicReference<StackNode<T>> head = new AtomicReference<>();
58+
private AtomicInteger counter = new AtomicInteger(0);
59+
60+
public void push(T value) {
61+
StackNode<T> newHeadNode = new StackNode<>(value);
62+
63+
while (true) {
64+
StackNode<T> currentHeadNode = head.get();
65+
newHeadNode.next = currentHeadNode;
66+
if (head.compareAndSet(currentHeadNode, newHeadNode)) {
67+
break;
68+
} else {
69+
LockSupport.parkNanos(1);
70+
}
71+
}
72+
counter.incrementAndGet();
73+
}
74+
75+
public T pop() {
76+
StackNode<T> currentHeadNode = head.get();
77+
StackNode<T> newHeadNode;
78+
79+
while (currentHeadNode != null) {
80+
newHeadNode = currentHeadNode.next;
81+
if (head.compareAndSet(currentHeadNode, newHeadNode)) {
82+
break;
83+
} else {
84+
LockSupport.parkNanos(1);
85+
currentHeadNode = head.get();
86+
}
87+
}
88+
counter.incrementAndGet();
89+
return currentHeadNode != null ? currentHeadNode.value : null;
90+
}
91+
92+
public int getCounter() {
93+
return counter.get();
94+
}
95+
}
96+
97+
public static class StandardStack<T> {
98+
private StackNode<T> head;
99+
private int counter = 0;
100+
101+
public synchronized void push(T value) {
102+
StackNode<T> newHead = new StackNode<>(value);
103+
newHead.next = head;
104+
head = newHead;
105+
counter++;
106+
}
107+
108+
public synchronized T pop() {
109+
if (head == null) {
110+
counter++;
111+
return null;
112+
}
113+
114+
T value = head.value;
115+
head = head.next;
116+
counter++;
117+
return value;
118+
}
119+
120+
public int getCounter() {
121+
return counter;
122+
}
123+
}
124+
125+
private static class StackNode<T> {
126+
public T value;
127+
public StackNode<T> next;
128+
129+
public StackNode(T value) {
130+
this.value = value;
131+
this.next = next;
132+
}
133+
}
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.jd.thread.LockFree;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
public class AtomicIntEx {
6+
public static void main(String[] args) throws InterruptedException {
7+
InventoryCounter inventoryCounter = new InventoryCounter();
8+
IncrementingThread incrementingThread = new IncrementingThread(inventoryCounter);
9+
DecrementingThread decrementingThread = new DecrementingThread(inventoryCounter);
10+
11+
incrementingThread.start();
12+
decrementingThread.start();
13+
14+
incrementingThread.join();
15+
decrementingThread.join();
16+
17+
System.out.println("We currently have " + inventoryCounter.getItems() + " items");
18+
}
19+
20+
public static class DecrementingThread extends Thread {
21+
22+
private InventoryCounter inventoryCounter;
23+
24+
public DecrementingThread(InventoryCounter inventoryCounter) {
25+
this.inventoryCounter = inventoryCounter;
26+
}
27+
28+
@Override
29+
public void run() {
30+
for (int i = 0; i < 10000; i++) {
31+
inventoryCounter.decrement();
32+
}
33+
}
34+
}
35+
36+
public static class IncrementingThread extends Thread {
37+
38+
private InventoryCounter inventoryCounter;
39+
40+
public IncrementingThread(InventoryCounter inventoryCounter) {
41+
this.inventoryCounter = inventoryCounter;
42+
}
43+
44+
@Override
45+
public void run() {
46+
for (int i = 0; i < 10000; i++) {
47+
inventoryCounter.increment();
48+
}
49+
}
50+
}
51+
52+
private static class InventoryCounter {
53+
private AtomicInteger items = new AtomicInteger(0);
54+
55+
public void increment() {
56+
items.incrementAndGet();
57+
}
58+
59+
public void decrement() {
60+
items.decrementAndGet();
61+
}
62+
63+
public int getItems() {
64+
return items.get();
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)