Skip to content

Commit daed709

Browse files
author
Rajeev Kumar Singh
committedJul 23, 2017
Lock Objects and AtomicVariables examples
1 parent 9f491e6 commit daed709

10 files changed

+65
-11
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.idea
33
out
44
*.iml
5+
*.class
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
import java.util.concurrent.locks.ReadWriteLock;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
16
/**
27
* Created by rajeevkumarsingh on 11/05/17.
38
*/
9+
class ReadWriteCounter {
10+
ReadWriteLock lock = new ReentrantReadWriteLock();
11+
12+
private int count = 0;
13+
14+
public int incrementAndGetCount() {
15+
lock.writeLock().lock();
16+
17+
try {
18+
count = count + 1;
19+
return count;
20+
} finally {
21+
lock.writeLock().unlock();
22+
}
23+
}
24+
25+
public int getCount() {
26+
lock.readLock().lock();
27+
try {
28+
return count;
29+
} finally {
30+
lock.readLock().unlock();
31+
}
32+
}
33+
}
34+
435
public class ReadWriteLockExample {
36+
37+
public static void main(String[] args) {
38+
ExecutorService executorService = Executors.newFixedThreadPool(3);
39+
40+
ReadWriteCounter counter = new ReadWriteCounter();
41+
42+
Runnable readTask = () -> {
43+
System.out.println(Thread.currentThread().getName() +
44+
" Read Task : " + counter.getCount());
45+
};
46+
47+
Runnable writeTask = () -> {
48+
System.out.println(Thread.currentThread().getName() +
49+
" Write Task : " + counter.incrementAndGetCount());
50+
};
51+
52+
executorService.submit(readTask);
53+
executorService.submit(readTask);
54+
55+
executorService.submit(writeTask);
56+
57+
executorService.submit(readTask);
58+
executorService.submit(readTask);
59+
}
560
}

‎java-lock-objects-and-atomic-variables/src/ReentrantLockExample.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ReentrantLockCounter {
1111

1212
private int count = 0;
1313

14+
// Thread Safe Increment
1415
public void increment() {
1516
lock.lock();
1617
try {
@@ -30,11 +31,13 @@ public int getCount() {
3031
public class ReentrantLockExample {
3132

3233
public static void main(String[] args) throws InterruptedException {
33-
ExecutorService executorService = Executors.newFixedThreadPool(2);
34+
ExecutorService executorService = Executors.newFixedThreadPool(5);
3435

3536
ReentrantLockCounter counter = new ReentrantLockCounter();
3637

37-
for(int i = 0; i < 1000; i++) {
38+
executorService.submit(() -> counter.increment());
39+
40+
for(int i = 0; i < 10; i++) {
3841
executorService.submit(() -> counter.increment());
3942
}
4043

‎java-lock-objects-and-atomic-variables/src/StampedLockExample.java

-5
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎java-thread-and-runnable-examples/src/ThreadJoinExample.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static void main(String[] args) {
1010
try {
1111
Thread.sleep(2000);
1212
} catch (InterruptedException e) {
13-
e.printStackTrace();
13+
throw new IllegalStateException(e);
1414
}
1515
System.out.println("Exiting Thread 1");
1616
});
@@ -21,7 +21,7 @@ public static void main(String[] args) {
2121
try {
2222
Thread.sleep(4000);
2323
} catch (InterruptedException e) {
24-
e.printStackTrace();
24+
throw new IllegalStateException(e);
2525
}
2626
System.out.println("Exiting Thread 2");
2727
});
@@ -33,7 +33,7 @@ public static void main(String[] args) {
3333
try {
3434
thread1.join(1000);
3535
} catch (InterruptedException e) {
36-
e.printStackTrace();
36+
throw new IllegalStateException(e);
3737
}
3838

3939
System.out.println("Waited enough! Starting Thread 2 now");
Binary file not shown.

‎java-thread-and-runnable-examples/src/ThreadSleepExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222
try {
2323
Thread.sleep(2000);
2424
} catch (InterruptedException e) {
25-
e.printStackTrace();
25+
throw new IllegalStateException(e);
2626
}
2727
}
2828
};

0 commit comments

Comments
 (0)
Please sign in to comment.