无意间看到此面试题,就临时写了几种写法,信号量的暂时还没去使用,待补充日后
/**
* 使用共享变量及其 volatile
*/
public class Demo {
volatile static int state = 0;
public static void main(String[] args) {
new Thread(() -> {
while (true) {
if (state % 3 == 0) {
System.out.println(1);
state ++;
}
}
}).start();
new Thread(() -> {
while (true) {
if (state % 3 == 1) {
System.out.println(2);
state ++;
}
}
}).start();
new Thread(() -> {
while (true) {
if (state % 3 == 2) {
System.out.println(3);
state ++;
}
}
}).start();
}
}
/**
* 使用条件队列
*/
public class Demo3 {
static int count = 0;
static Lock lock = new ReentrantLock();
static Condition conditionA = lock.newCondition();
static Condition conditionB = lock.newCondition();
static Condition conditionC = lock.newCondition();
public static void main(String[] args) {
new Thread(() -> {
try {
lock.lock();
while (count != 21) {
while (count % 3 != 0) {
conditionA.await();
}
System.out.println("A");
count ++;
conditionB.signal();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
try {
lock.lock();
while (count != 21) {
while (count % 3 != 1) {
conditionB.await();
}
System.out.println("B");
count ++;
conditionC.signal();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
try {
lock.lock();
while (count != 21) {
while (count % 3 != 2) {
conditionC.await();
}
System.out.println("C");
count ++;
conditionA.signal();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
}
}
/**
* 先获取前一个,保证后一个在前一个释放锁之后再去执行响应逻辑
* 对应三个对象
*/
public class Demo2 {
static Object A = new Object();
static Object B = new Object();
static Object C = new Object();
static int count = 0;
public static void main(String[] args) throws InterruptedException {
Thread tA = new Thread(() -> {
while (count != 21) {
synchronized (C) {
synchronized (A) {
System.out.println("A");
A.notifyAll();
count ++;
}
try {
C.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread tB = new Thread(() -> {
while (count != 21) {
synchronized (A) {
synchronized (B) {
System.out.println("B");
B.notifyAll();
count ++;
}
try {
A.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread tC = new Thread(() -> {
while (count != 21) {
synchronized (B) {
synchronized (C) {
System.out.println("C");
C.notifyAll();
count ++;
}
try {
B.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
tA.start();
tB.sleep(10);
tB.start();
tC.sleep(10);
tC.start();
}
}
/**
* 巧用 Semaphore
*/
public class Demo4 {
static Semaphore A = new Semaphore(1);
static Semaphore B = new Semaphore(0);
static Semaphore C = new Semaphore(0);
public static void main(String[] args) {
new Thread(() -> {
while (true) {
try {
A.acquire();
System.out.println("A");
B.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
while (true) {
try {
B.acquire();
System.out.println("B");
C.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
while (true) {
try {
C.acquire();
System.out.println("C");
A.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}