Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 94ce568

Browse files
author
someone-1
committed
addded locks
1 parent 1515583 commit 94ce568

File tree

4 files changed

+102
-58
lines changed

4 files changed

+102
-58
lines changed

‎src/main/java/barrier/CountDownLatch.java‎

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package barrier;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReadWriteLock;
5+
import java.util.concurrent.locks.ReentrantReadWriteLock;
6+
7+
public class CountDownLatch {
8+
ReadWriteLock lock = new ReentrantReadWriteLock();
9+
private Lock rLock = lock.readLock();
10+
private Lock wLock = lock.writeLock();
11+
private int count;
12+
13+
14+
public CountDownLatch(int count){
15+
this.count = count;
16+
}
17+
18+
19+
public synchronized void countDown(){
20+
System.out.println(count + " -count in " + Thread.currentThread().getName());
21+
count--;
22+
this.notifyAll();
23+
}
24+
25+
26+
public int getCount(){
27+
return this.count;
28+
}
29+
30+
31+
public void await() throws InterruptedException {
32+
while (count > 0) {
33+
wait();
34+
}
35+
}
36+
37+
}

‎src/main/java/dataStructures/Hashmap.java‎

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dataStructures;
22

3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
36
public class Hashmap <K, V>{
47

58
private int bucketCount = 16;
@@ -10,7 +13,7 @@ public void insert(K key, V val){
1013
Bucket<K, V> bucket;
1114
int ind = getIndex(key);
1215
if(buckets[ind] == null){
13-
bucket = new Bucket<K, V>();
16+
bucket = new Bucket<>();
1417
buckets[ind] = bucket;
1518
}else {
1619
bucket = buckets[ind];
@@ -30,6 +33,8 @@ public V get(K key){
3033
return node.val;
3134
}
3235

36+
// public void replace(K key, V oldVal, )
37+
3338
public V delete(K key){
3439
int ind = getIndex(key);
3540
if(buckets[ind] == null){
@@ -52,59 +57,78 @@ private int getIndex(K key){
5257
class Bucket<K, V>{
5358
private Node<K, V> start;
5459
private Node<K, V> end;
60+
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
61+
private Lock rLock = lock.readLock();
62+
private Lock wLock = lock.writeLock();
5563

5664
/*
5765
* adds new node if key is not present
5866
* updated value of node if key is present
5967
* */
60-
public void updNode(Node<K, V> node){
61-
if(start == null){
62-
start = node;
63-
end = node;
64-
return;
65-
}
66-
Node<K, V> oldNode = getNode(node.key);
67-
if(oldNode == null) {
68-
end.next = node;
69-
node.prev = end;
70-
end = node;
71-
return;
68+
void updNode(Node<K, V> node){
69+
wLock.lock();
70+
try {
71+
if (start == null) {
72+
start = node;
73+
end = node;
74+
return;
75+
}
76+
Node<K, V> oldNode = getNode(node.key);
77+
if (oldNode == null) {
78+
end.next = node;
79+
node.prev = end;
80+
end = node;
81+
return;
82+
}
83+
oldNode.val = node.val;
84+
}finally {
85+
wLock.unlock();
7286
}
73-
oldNode.val = node.val;
7487
}
7588

7689
/*
7790
* returns node if node.key.equals(key)
7891
* else returns null
7992
* */
80-
public Node<K,V> getNode(K key){
81-
Node<K,V> node = start;
82-
while (node != null){
83-
if(node.key.equals(key)){
84-
return node;
93+
Node<K,V> getNode(K key){
94+
rLock.lock();
95+
try {
96+
Node<K, V> node = start;
97+
while (node != null) {
98+
if (node.key.equals(key)) {
99+
return node;
100+
}
101+
node = node.next;
85102
}
86-
node = node.next;
103+
return null;
104+
}finally {
105+
rLock.unlock();
87106
}
88-
return null;
89107
}
90108

91109

92-
public Node<K, V> delNode(K key){
93-
Node<K,V> node = getNode(key);
94-
if(node != null){
95-
if (start == node){
96-
start = node.next;
97-
}
98-
if (end == node){
99-
end = node.prev;
100-
}
101-
Node<K, V> nxtNode = node.next;
102-
Node<K, V> prevNode = node.prev;
103-
if(prevNode != null){
104-
prevNode.next = nxtNode;
110+
Node<K, V> delNode(K key){
111+
wLock.lock();
112+
try {
113+
114+
Node<K, V> node = getNode(key);
115+
if (node != null) {
116+
if (start == node) {
117+
start = node.next;
118+
}
119+
if (end == node) {
120+
end = node.prev;
121+
}
122+
Node<K, V> nxtNode = node.next;
123+
Node<K, V> prevNode = node.prev;
124+
if (prevNode != null) {
125+
prevNode.next = nxtNode;
126+
}
105127
}
128+
return node;
129+
}finally {
130+
wLock.unlock();
106131
}
107-
return node;
108132
}
109133
}
110134

@@ -113,7 +137,7 @@ class Node<K, V>{
113137
Node<K, V> prev;
114138
K key;
115139
V val;
116-
publicNode(K key, V val){
140+
Node(K key, V val){
117141
this.val = val;
118142
this.key = key;
119143
next = null;

‎src/test/java/barrier/TestCountDownLatch.java‎ renamed to ‎src/test/java/barrier/TestFakeCountDownLatch.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public void testCountDown() throws InterruptedException {
1616
latchSingleThread.countDown();
1717
latchSingleThread.countDown();
1818
Assertions.assertEquals(threadCount-2, latchSingleThread.getCount());
19+
20+
1921
CountDownLatch latch = new CountDownLatch(threadCount);
2022
java.util.concurrent.CountDownLatch realLatch = new
2123
java.util.concurrent.CountDownLatch(threadCount - diff);
22-
ExecutorService service = Executors.newFixedThreadPool(10);
24+
ExecutorService service = Executors.newFixedThreadPool(threadCount);
2325
for (int i = 0; i < threadCount; i++) {
2426
service.submit(() -> {
2527
try {
@@ -29,9 +31,11 @@ public void testCountDown() throws InterruptedException {
2931
}
3032
latch.countDown();
3133
realLatch.countDown();
34+
System.out.println(latch.getCount() + " - count " + threadCount);
3235
});
3336
}
3437
realLatch.await();
38+
latch.await();
3539
Assertions.assertEquals(diff, latch.getCount());
3640
}
3741

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /