混合型自旋鎖 純自旋鎖在競爭激烈的情況下會頻繁的自旋浪費CPU資源, 所以一般使用混合型自旋鎖進行改進,在嘗試了一定次數後進入休眠狀態
public class HybridSpinLock {
// 自旋次數
private static final int TRY_TIMES = 20;
// 最多等待多長時間
private static final int MAX_WAIT_MILLIS_ECONDS = 1000;
// 鎖標誌
private AtomicReference<HybridSpinLock> obj = new AtomicReference<HybridSpinLock>();
// 當前等待的線程數量
private AtomicInteger waitCount = new AtomicInteger(0);
public void lock() {
int times = 0;
while (!obj.compareAndSet(null, this)) {
times++;
if (times % TRY_TIMES == 0) {
try {
synchronized (obj) {
System.out.println("線程等待");
waitCount.addAndGet(1);
obj.wait(MAX_WAIT_MILLIS_ECONDS);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 通過設置鎖標誌爲null釋放鎖
* 如果有等待的線程,喚醒一個
*/
public void unlock() {
obj.set(null);
// 等待線程的數量
int waitCountNum = waitCount.get();
while (true) {
if (waitCountNum > 0
&& waitCount.compareAndSet(waitCountNum, waitCountNum - 1)) {
// 當有等待的線程時喚醒一個
synchronized (obj) {
System.out.println("喚醒一個線程");
obj.notify();
}
break;
}
waitCountNum = waitCount.get();
//如果沒有等待的線程跳出循環
if (waitCountNum <= 0)
break;
}
}
public static void main(String[] args) {
class Task implements Runnable {
private HybridSpinLock spinLock = new HybridSpinLock();
private long count = 0;
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
spinLock.lock();
count += 1;
count += 1;
// 模擬耗時操作,如果沒有耗時操作,
//線程可能不會超出自旋次數,不進入等待狀態
//嘗試註釋掉sleep後運行
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
spinLock.unlock();
}
}
}
public long getValue() {
return count;
}
}
// 線程數量
final int THREAD_NUM = 10;
Task task = new Task();
List<Thread> threads = new ArrayList<Thread>();
try {
for (int i = 0; i < THREAD_NUM; i++) {
Thread t = new Thread(task);
t.start();
threads.add(t);
}
for (Thread t : threads) {
t.join();
}
System.out.println("結果:" + task.getValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}