练习JUC的题目
语言:Java
来源:LeetCode
打印奇偶数
使用sem比lock要简单一些
class ZeroEvenOdd {
private int n;
Semaphore zero = new Semaphore(1);
Semaphore even = new Semaphore(0);
Semaphore odd = new Semaphore(0);
volatile int flag = 1;
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for (int i = 0; i < n; i++) {
zero.acquire();
printNumber.accept(0);
if((flag & 1) == 1){
odd.release();
}else{
even.release();
}
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for (int i = 2; i <= n; i+=2) {
even.acquire();
printNumber.accept(i);
flag = 1;
zero.release();
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; i+=2) {
odd.acquire();
printNumber.accept(i);
flag = 2;
zero.release();
}
}
}
设计有限阻塞队列
代码:
class BoundedBlockingQueue {
private volatile int size;
private List<Integer> list;
private ReentrantLock lock;
private Condition c1;
private Condition c2;
public BoundedBlockingQueue(int capacity) {
size = capacity;
list = new LinkedList<>();
lock = new ReentrantLock();
c1 = lock.newCondition();
c2 = lock.newCondition();
}
public void enqueue(int element) throws InterruptedException {
try{
lock.lock();
while (size <= list.size()) {
c1.await();
}
list.add(element);
c2.signal();
}finally {
lock.unlock();
}
}
public int dequeue() throws InterruptedException {
int res = 0;
try{
lock.lock();
while (list.size() == 0 ) {
c2.await();
}
res = list.remove(0);
c1.signal();
}finally {
lock.unlock();
}
return res;
}
public int size() {
return list.size();
}
}
红绿灯路口
class TrafficLight {
private Semaphore s;
private boolean isGreen;
public TrafficLight() {
s = new Semaphore(1);
isGreen = true;
}
public void carArrived(
int carId, // ID of the car
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
int direction, // Direction of the car
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
Runnable crossCar // Use crossCar.run() to make car cross the intersection
) {
s.acquireUninterruptibly();
if((roadId == 1 && !isGreen) || (roadId == 2 && isGreen)) {
turnGreen.run();
isGreen = !isGreen;
}
crossCar.run();
s.release();
}
}
哲学家进餐
5 个沉默寡言的哲学家围坐在圆桌前,每人面前一盘意面。叉子放在哲学家之间的桌面上。(5 个哲学家,5 根叉子)
所有的哲学家都只会在思考和进餐两种行为间交替。哲学家只有同时拿到左边和右边的叉子才能吃到面,而同一根叉子在同一时间只能被一个哲学家使用。每个哲学家吃完面后都需要把叉子放回桌面以供其他哲学家吃面。只要条件允许,哲学家可以拿起左边或者右边的叉子,但在没有同时拿到左右叉子时不能进食。
假设面的数量没有限制,哲学家也能随便吃,不需要考虑吃不吃得下。
设计一个进餐规则(并行算法)使得每个哲学家都不会挨饿;也就是说,在没有人知道别人什么时候想吃东西或思考的情况下,每个哲学家都可以在吃饭和思考之间一直交替下去。
class DiningPhilosophers {
Semaphore[] locks = new Semaphore[]{
new Semaphore(1),
new Semaphore(0),
new Semaphore(0),
new Semaphore(0),
new Semaphore(0)
};
public DiningPhilosophers() {
}
// call the run() method of any runnable to execute its code
public void wantsToEat(int philosopher,
Runnable pickLeftFork,
Runnable pickRightFork,
Runnable eat,
Runnable putLeftFork,
Runnable putRightFork) throws InterruptedException {
// 一个个吃
locks[philosopher].acquire();
pickLeftFork.run();
pickRightFork.run();
eat.run();
putLeftFork.run();
putRightFork.run();
locks[(philosopher + 1) % 5].release();
}
}