Skip to content

Commit b6344f4

Browse files
committed
OK XFORG
1 parent eef69dd commit b6344f4

9 files changed

Lines changed: 159 additions & 44 deletions

File tree

.idea/workspace.xml

Lines changed: 87 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.26 KB
Binary file not shown.
809 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/com/xforg/concurrency/AtomicityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import java.util.concurrent.*;
33

44
public class AtomicityTest implements Runnable {
5-
private int i = 0;
5+
private int i = 0;
66
public int getValue() { return i; }
77
private synchronized void evenIncrement() {
88
i++; i++;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.xforg.concurrency;//: concurrency/SerialNumberChecker.java
2+
// Operations that may seem safe are not,
3+
// when threads are present.
4+
// {Args: 4}
5+
import java.util.concurrent.*;
6+
7+
// Reuses storage so we don't run out of memory:
8+
class CircularSet {
9+
private int[] array;
10+
private int len;
11+
private int index = 0;
12+
public CircularSet(int size) {
13+
array = new int[size];
14+
len = size;
15+
// Initialize to a value not produced
16+
// by the SerialNumberGenerator:
17+
for(int i = 0; i < size; i++)
18+
array[i] = -1;
19+
}
20+
public synchronized void add(int i) {
21+
array[index] = i;
22+
// Wrap index and write over old elements:
23+
index = ++index % len;
24+
}
25+
public synchronized boolean contains(int val) {
26+
for(int i = 0; i < len; i++)
27+
if(array[i] == val) return true;
28+
return false;
29+
}
30+
}
31+
32+
public class SerialNumberChecker {
33+
private static final int SIZE = 10;
34+
private static CircularSet serials =
35+
new CircularSet(1000);
36+
private static ExecutorService exec =
37+
Executors.newCachedThreadPool();
38+
static class SerialChecker implements Runnable {
39+
public void run() {
40+
while(true) {
41+
int serial =
42+
SerialNumberGenerator.nextSerialNumber();
43+
if(serials.contains(serial)) {
44+
System.out.println("Duplicate: " + serial);
45+
System.exit(0);
46+
}
47+
serials.add(serial);
48+
}
49+
}
50+
}
51+
public static void main(String[] args) throws Exception {
52+
for(int i = 0; i < SIZE; i++)
53+
exec.execute(new SerialChecker());
54+
// Stop after n seconds if there's an argument:
55+
if(args.length > 0) {
56+
TimeUnit.SECONDS.sleep(new Integer(args[0]));
57+
System.out.println("No duplicates detected");
58+
System.exit(0);
59+
}
60+
}
61+
} /* Output: (Sample)
62+
Duplicate: 8468656
63+
*///:~
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.xforg.concurrency;//: concurrency/SerialNumberGenerator.java
2+
3+
public class SerialNumberGenerator {
4+
private static volatile int serialNumber = 0;
5+
public static int nextSerialNumber() {/*问题在于nextSerialNumber在没有同步的情况下对共享可变值进行了访问*/
6+
return serialNumber++; // Not thread-safe
7+
}
8+
} ///:~

0 commit comments

Comments
 (0)