CAS¶
public class SpinLock {
AtomicReference<Thread> atomicReference = new AtomicReference<>();
public void myLock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName() + " --> mylock");
while (!atomicReference.compareAndSet(null, thread)){
}
}
public void myUnLock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName() + " --> myunlock");
atomicReference.compareAndSet(thread, null);
}
}