Skip to content

Commit b855c63

Browse files
author
Rajeev Kumar Singh
committed
Readme
1 parent daed709 commit b855c63

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Java Concurrency/Multithreading Examples
22

3-
## Releavant Tutorials
3+
## Relevant Tutorials
44

55
1. [Java Concurrency / Multithreading Basics](https://www.callicoder.com/java-concurrency-multithreading-basics/)
66

@@ -10,4 +10,6 @@
1010

1111
4. [Java Callable and Future Tutorial](https://www.callicoder.com/java-callable-and-future-tutorial/)
1212

13-
5. [Java Concurrency issues and Synchronization](https://www.callicoder.com/java-concurrency-issues-and-thread-synchronization/)
13+
5. [Java Concurrency issues and Synchronization](https://www.callicoder.com/java-concurrency-issues-and-thread-synchronization/)
14+
15+
6. [Java Locks and Atomic Variables](https://www.callicoder.com/java-locks-and-atomic-variables-tutorial/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
/**
7+
* Created by rajeevkumarsingh on 24/07/17.
8+
*/
9+
10+
class AtomicCounter {
11+
private AtomicInteger count = new AtomicInteger(0);
12+
13+
public int incrementAndGet() {
14+
return count.incrementAndGet();
15+
}
16+
17+
public int getCount() {
18+
return count.get();
19+
}
20+
}
21+
public class AtomicIntegerExample {
22+
public static void main(String[] args) throws InterruptedException {
23+
ExecutorService executorService = Executors.newFixedThreadPool(2);
24+
25+
AtomicCounter atomicCounter = new AtomicCounter();
26+
27+
for(int i = 0; i < 1000; i++) {
28+
executorService.submit(() -> atomicCounter.incrementAndGet());
29+
}
30+
31+
System.out.println("Final Count is : " + atomicCounter.getCount());
32+
33+
executorService.shutdown();
34+
executorService.awaitTermination(60, TimeUnit.SECONDS);
35+
}
36+
}

java-lock-objects-and-atomic-variables/src/ReadWriteLockExample.java

+2
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ public static void main(String[] args) {
5656

5757
executorService.submit(readTask);
5858
executorService.submit(readTask);
59+
60+
executorService.shutdown();
5961
}
6062
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ public static void main(String[] args) throws InterruptedException {
4545
executorService.awaitTermination(60, TimeUnit.SECONDS);
4646

4747
System.out.println("Final count is : " + counter.getCount());
48+
4849
}
4950
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
class ReentrantLockCounterIncrement {
7+
private final ReentrantLock lock = new ReentrantLock();
8+
9+
private int count = 0;
10+
11+
public int incrementAndGet() {
12+
// Check if lock is currently acquired by any thread
13+
System.out.println("IsLocked : " + lock.isLocked());
14+
15+
// Check if lock is acquired by the current thread itself.
16+
System.out.println("IsHeldByCurrentThread : " + lock.isHeldByCurrentThread());
17+
18+
// Try to acquire the lock
19+
boolean isAcquired;
20+
try {
21+
isAcquired = lock.tryLock(1, TimeUnit.SECONDS);
22+
System.out.println("Lock Acquired : " + isAcquired + "\n");
23+
} catch (InterruptedException e) {
24+
throw new IllegalStateException(e);
25+
}
26+
27+
if(isAcquired) {
28+
try {
29+
Thread.sleep(2000);
30+
count = count + 1;
31+
} catch (InterruptedException e) {
32+
throw new IllegalStateException(e);
33+
} finally {
34+
lock.unlock();
35+
}
36+
}
37+
return count;
38+
}
39+
}
40+
41+
public class ReentrantLockMethodsExample {
42+
43+
public static void main(String[] args) {
44+
ExecutorService executorService = Executors.newFixedThreadPool(2);
45+
46+
ReentrantLockCounterIncrement lockCounterIncrement = new ReentrantLockCounterIncrement();
47+
48+
executorService.submit(() -> {
49+
System.out.println("IncrementCount (First Thread) : " +
50+
lockCounterIncrement.incrementAndGet() + "\n");
51+
});
52+
53+
executorService.submit(() -> {
54+
System.out.println("IncrementCount (Second Thread) : " +
55+
lockCounterIncrement.incrementAndGet() + "\n");
56+
});
57+
58+
executorService.shutdown();
59+
60+
}
61+
}

0 commit comments

Comments
 (0)