一、內置鎖
- 同步:synchronized
- 協作:Object # wait/notify/notifyAll
public class PrintABC {
/**
* 打印鎖,同一時刻僅有一個任務可以持有此鎖
*/
private static Object lock = new Object();
private static int state = 1;
public static void main(String args[]) {
new Thread(new ATask()).start();
new Thread(new BTask()).start();
new Thread(new CTask()).start();
}
static class ATask implements Runnable {
@Override
public void run() {
synchronized (lock) {
while (state <= 100) {
if (state % 3 == 1) {
System.out.print('A');
state++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
static class BTask implements Runnable {
@Override
public void run() {
synchronized (lock) {
while (state <= 100) {
if (state % 3 == 2) {
System.out.print('B');
state++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
static class CTask implements Runnable {
@Override
public void run() {
synchronized (lock) {
while (state <= 100) {
if (state % 3 == 0) {
System.out.print('C');
state++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
二、顯式鎖
- 同步:lock/unlock
- 協作:Condition # await/signal/signalAll
public class PrintABC {
/**
* 打印鎖,同一時刻僅有一個任務可以持有此鎖
*/
private static Lock lock = new ReentrantLock();
private static Condition aCondition = lock.newCondition();
private static Condition bCondition = lock.newCondition();
private static Condition cCondition = lock.newCondition();
private static int state = 1;
public static void main(String args[]) {
new Thread(new ATask()).start();
new Thread(new BTask()).start();
new Thread(new CTask()).start();
}
static class ATask implements Runnable {
@Override
public void run() {
try {
lock.lock();
while (state <= 100) {
if (state % 3 == 1) {
System.out.print('A');
state++;
bCondition.signalAll();
} else {
try {
aCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} finally {
lock.unlock();
}
}
}
static class BTask implements Runnable {
@Override
public void run() {
try {
lock.lock();
while (state <= 100) {
if (state % 3 == 2) {
System.out.print('B');
state++;
cCondition.signalAll();
} else {
try {
bCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} finally {
lock.unlock();
}
}
}
static class CTask implements Runnable {
@Override
public void run() {
try {
lock.lock();
while (state <= 100) {
if (state % 3 == 0) {
System.out.print('C');
state++;
aCondition.signalAll();
} else {
try {
cCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} finally {
lock.unlock();
}
}
}
}
三、信號量
public class PrintABC {
private static Semaphore A = new Semaphore(1);
private static Semaphore B = new Semaphore(1);
private static Semaphore C = new Semaphore(1);
private static int state = 1;
public static void main(String args[]) throws InterruptedException {
B.acquire();
C.acquire();
new Thread(new ATask()).start();
new Thread(new BTask()).start();
new Thread(new CTask()).start();
}
static class ATask implements Runnable {
@Override
public void run() {
try {
while (state <= 100) {
A.acquire();
System.out.print('A');
B.release();
state++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class BTask implements Runnable {
@Override
public void run() {
try {
while (state <= 100) {
B.acquire();
System.out.print('B');
C.release();
state++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class CTask implements Runnable {
@Override
public void run() {
try {
while (state <= 100) {
C.acquire();
System.out.print('C');
A.release();
state++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}