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 5f347cd

Browse files
committed
feat: add leetcode question #1279 #1188
1 parent 5ad35b8 commit 5f347cd

File tree

7 files changed

+283
-0
lines changed

7 files changed

+283
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1188;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.Lock;
7+
import java.util.concurrent.locks.ReentrantLock;
8+
9+
/**
10+
* <pre>
11+
* author: dhl
12+
* date : 2021年1月2日
13+
* desc :
14+
* </pre>
15+
*/
16+
class BoundedBlockingQueueLock {
17+
private final int capacity;
18+
private final Lock lock = new ReentrantLock();
19+
private final Condition full = lock.newCondition();
20+
private final Condition empty = lock.newCondition();
21+
private final List<Integer> queue = new ArrayList<>();
22+
23+
public BoundedBlockingQueueLock(int capacity) {
24+
this.capacity = capacity;
25+
}
26+
27+
public void enqueue(int element) throws InterruptedException {
28+
lock.lock();
29+
try {
30+
while (queue.size() >= capacity) {
31+
full.await();
32+
}
33+
queue.add(0, element);
34+
empty.signalAll();
35+
} finally {
36+
lock.unlock();
37+
}
38+
}
39+
40+
public int dequeue() throws InterruptedException {
41+
lock.lock();
42+
int element = 0;
43+
try {
44+
while (queue.size() <= 0) {
45+
empty.await();
46+
}
47+
element = queue.get(queue.size() - 1);
48+
queue.remove(queue.size() - 1);
49+
full.signalAll();
50+
} finally {
51+
lock.unlock();
52+
}
53+
return element;
54+
}
55+
56+
public int size() {
57+
return queue.size();
58+
}
59+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1188;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* <pre>
8+
* author: dhl
9+
* date : 2021年1月2日
10+
* desc :
11+
* </pre>
12+
*/
13+
class BoundedBlockingQueueSync {
14+
private int capacity;
15+
private final Object LOCK = new Object();
16+
private final List<Integer> queue = new ArrayList<>();
17+
18+
public BoundedBlockingQueueSync(int capacity) {
19+
this.capacity = capacity;
20+
}
21+
22+
public void enqueue(int element) throws InterruptedException {
23+
synchronized (LOCK) {
24+
while (queue.size() >= capacity) {
25+
LOCK.wait();
26+
}
27+
queue.add(0, element);
28+
LOCK.notifyAll();
29+
}
30+
}
31+
32+
public int dequeue() throws InterruptedException {
33+
int element = 0;
34+
synchronized (LOCK) {
35+
while (queue.size() <= 0) {
36+
LOCK.wait();
37+
}
38+
element = queue.get(queue.size() - 1);
39+
queue.remove(queue.size() - 1);
40+
LOCK.notifyAll();
41+
}
42+
return element;
43+
}
44+
45+
public int size() {
46+
return queue.size();
47+
}
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1279;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
/**
6+
* <pre>
7+
* author: dhl
8+
* date : 2021年1月2日
9+
* desc :
10+
* </pre>
11+
*/
12+
class TrafficLightCAS {
13+
14+
private final AtomicInteger atomic = new AtomicInteger();
15+
private boolean road1GreeOn = false;
16+
17+
public TrafficLightCAS() {
18+
19+
}
20+
21+
public void carArrived(
22+
int carId, // ID of the car
23+
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
24+
int direction, // Direction of the car
25+
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
26+
Runnable crossCar // Use crossCar.run() to make car cross the intersection
27+
) {
28+
try {
29+
while (!atomic.compareAndSet(0, 1)) Thread.sleep(1);
30+
31+
if (roadId == 1 && road1GreeOn) {
32+
turnGreen.run();
33+
road1GreeOn = false;
34+
} else if (roadId == 2 && !road1GreeOn) {
35+
turnGreen.run();
36+
road1GreeOn = true;
37+
}
38+
crossCar.run();
39+
40+
while (!atomic.compareAndSet(1, 0)) Thread.sleep(1);
41+
} catch (Exception e) {
42+
43+
}
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1279;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* <pre>
8+
* author: dhl
9+
* date : 2021年1月2日
10+
* desc :
11+
* </pre>
12+
*/
13+
class TrafficLightLock {
14+
private final Lock lock = new ReentrantLock();
15+
private boolean road1GreenOn = false;
16+
17+
public TrafficLightLock() {
18+
19+
}
20+
21+
public void carArrived(
22+
int carId, // ID of the car
23+
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
24+
int direction, // Direction of the car
25+
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
26+
Runnable crossCar // Use crossCar.run() to make car cross the intersection
27+
) {
28+
lock.lock();
29+
try {
30+
if (roadId == 1 && road1GreenOn) {
31+
turnGreen.run();
32+
road1GreenOn = false;
33+
} else if (roadId == 2 && !road1GreenOn) {
34+
turnGreen.run();
35+
road1GreenOn = true;
36+
}
37+
crossCar.run();
38+
39+
} catch (Exception e) {
40+
41+
} finally {
42+
lock.unlock();
43+
}
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1279;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
/**
6+
* <pre>
7+
* author: dhl
8+
* date : 2021年1月2日
9+
* desc :
10+
* </pre>
11+
*/
12+
class TrafficLightSemaphore {
13+
private final Semaphore semaphore = new Semaphore(1);
14+
private boolean road1GreenOn = false;
15+
16+
public TrafficLightSemaphore() {
17+
18+
}
19+
20+
public void carArrived(
21+
int carId, // ID of the car
22+
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
23+
int direction, // Direction of the car
24+
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
25+
Runnable crossCar // Use crossCar.run() to make car cross the intersection
26+
) {
27+
try {
28+
semaphore.acquire();
29+
if (roadId == 1 && road1GreenOn) {
30+
turnGreen.run();
31+
road1GreenOn = false;
32+
} else if (roadId == 2 && !road1GreenOn) {
33+
turnGreen.run();
34+
road1GreenOn = true;
35+
}
36+
crossCar.run();
37+
} catch (Exception e) {
38+
39+
} finally {
40+
semaphore.release();
41+
}
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1279;
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2021年1月2日
7+
* desc :
8+
* </pre>
9+
*/
10+
class TrafficLightSynchronized {
11+
private final Object LOCK = new Object();
12+
private boolean road1GreenOn = false;
13+
14+
public TrafficLightSynchronized() {
15+
16+
}
17+
18+
public void carArrived(
19+
int carId, // ID of the car
20+
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
21+
int direction, // Direction of the car
22+
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
23+
Runnable crossCar // Use crossCar.run() to make car cross the intersection
24+
) {
25+
synchronized (LOCK) {
26+
try {
27+
if (roadId == 1 && road1GreenOn) {
28+
turnGreen.run();
29+
road1GreenOn = false;
30+
} else if (roadId == 2 && !road1GreenOn) {
31+
turnGreen.run();
32+
road1GreenOn = true;
33+
}
34+
crossCar.run();
35+
} catch (Exception e) {
36+
37+
}
38+
}
39+
}
40+
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/template/sort/SelectSort.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hi.dhl.algorithms.other.template.sort;
22

3+
import java.util.Queue;
4+
35
/**
46
* <pre>
57
* author: dhl
@@ -26,6 +28,7 @@ private void selectSort(int[] nums) {
2628
min = j;
2729
}
2830
}
31+
2932
swap(nums, i, min);
3033
}
3134
}

0 commit comments

Comments
(0)

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