Skip to content

Commit eef69dd

Browse files
committed
OK see you
1 parent 723c98b commit eef69dd

13 files changed

Lines changed: 266 additions & 103 deletions

.idea/workspace.xml

Lines changed: 111 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.01 KB
Binary file not shown.
2.2 KB
Binary file not shown.
1.74 KB
Binary file not shown.
713 Bytes
Binary file not shown.
519 Bytes
Binary file not shown.
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.xforg.concurrency;//: concurrency/AtomicityTest.java
2+
import java.util.concurrent.*;
3+
4+
public class AtomicityTest implements Runnable {
5+
private int i = 0;
6+
public int getValue() { return i; }
7+
private synchronized void evenIncrement() {
8+
i++; i++;
9+
}
10+
public void run() {
11+
while(true)
12+
evenIncrement();
13+
}
14+
public static void main(String[] args) {
15+
ExecutorService exec = Executors.newCachedThreadPool();
16+
AtomicityTest at = new AtomicityTest();
17+
exec.execute(at);
18+
while(true) {
19+
int val = at.getValue();
20+
if(val % 2 != 0) {
21+
System.out.println(val);
22+
System.exit(0);
23+
}
24+
}
25+
}
26+
} /* Output: (Sample)
27+
191583767
28+
*///:~
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.xforg.concurrency;//: concurrency/AttemptLocking.java
2+
// Locks in the concurrent library allow you
3+
// to give up on trying to acquire a lock.
4+
import java.util.concurrent.*;
5+
import java.util.concurrent.locks.*;
6+
7+
public class AttemptLocking {
8+
private ReentrantLock lock = new ReentrantLock();
9+
public void untimed() {
10+
boolean captured = lock.tryLock();
11+
try {
12+
System.out.println("tryLock(): " + captured);
13+
} finally {
14+
if(captured)
15+
lock.unlock();
16+
}
17+
}
18+
public void timed() {
19+
boolean captured = false;
20+
try {
21+
captured = lock.tryLock(2, TimeUnit.SECONDS);
22+
} catch(InterruptedException e) {
23+
throw new RuntimeException(e);
24+
}
25+
try {
26+
System.out.println("tryLock(2, TimeUnit.SECONDS): " +
27+
captured);
28+
} finally {
29+
if(captured)
30+
lock.unlock();
31+
}
32+
}
33+
public static void main(String[] args) {
34+
final AttemptLocking al = new AttemptLocking();
35+
al.untimed(); // True -- lock is available
36+
al.timed(); // True -- lock is available
37+
// Now create a separate task to grab the lock:
38+
new Thread() {
39+
{ setDaemon(true); }
40+
public void run() {
41+
al.lock.lock();
42+
System.out.println(" HAHA~Thread acquired CPU ");
43+
}
44+
}.start();
45+
Thread.yield(); // Give the 2nd task a chance
46+
al.untimed(); // False -- lock grabbed by task
47+
al.timed(); // False -- lock grabbed by task
48+
}
49+
} /* Output:
50+
tryLock(): true
51+
tryLock(2, TimeUnit.SECONDS): true
52+
acquired
53+
tryLock(): false
54+
tryLock(2, TimeUnit.SECONDS): false
55+
*///:~
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.xforg.concurrency;//: concurrency/EvenChecker.java
2+
import java.util.concurrent.*;
3+
4+
public class EvenChecker implements Runnable {
5+
private IntGenerator generator;
6+
private final int id;
7+
public EvenChecker(IntGenerator g, int ident) {
8+
generator = g;
9+
id = ident;
10+
}
11+
public void run() {
12+
while(!generator.isCanceled()) {
13+
int val = generator.next();
14+
if(val % 2 != 0) {
15+
System.out.println(val + " not even!");
16+
generator.cancel(); // Cancels all EvenCheckers
17+
}
18+
}
19+
}
20+
// Test any type of IntGenerator:
21+
public static void test(IntGenerator gp, int count) {
22+
System.out.println("Press Control-C to exit");
23+
ExecutorService exec = Executors.newCachedThreadPool();
24+
for(int i = 0; i < count; i++)
25+
exec.execute(new EvenChecker(gp, i));
26+
exec.shutdown();
27+
}
28+
// Default value for count:
29+
public static void test(IntGenerator gp) {
30+
test(gp, 10);
31+
}
32+
} ///:~

0 commit comments

Comments
 (0)