forked from andrei-punko/java-interview-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomLock.java
More file actions
29 lines (25 loc) · 733 Bytes
/
CustomLock.java
File metadata and controls
29 lines (25 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package by.andd3dfx.multithreading.lock;
/**
* Custom lock with simple implementation but without reentrancy support
* <p>
* See https://jenkov.com/tutorials/java-concurrency/locks.html
*
* @see <a href="https://youtu.be/QdvsNhf5FI4">Video solution</a>
*/
public class CustomLock implements Lock {
private boolean isLocked = false;
@Override
public synchronized void lock() throws InterruptedException {
while (isLocked) {
wait();
}
isLocked = true;
System.out.println("Locked...");
}
@Override
public synchronized void unlock() {
isLocked = false;
System.out.println("Unlocked...");
notify();
}
}