commit 44f0966f15f50fc6d979909c225c4409cacbbce3 Author: Wtd <1658714322@qq.com> Date: Sun Oct 29 09:05:34 2023 +0800 自旋锁 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d9a414b --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.wtd + SpinLock + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + diff --git a/src/main/java/com/wtd/SpinLockDemo.java b/src/main/java/com/wtd/SpinLockDemo.java new file mode 100644 index 0000000..a746daf --- /dev/null +++ b/src/main/java/com/wtd/SpinLockDemo.java @@ -0,0 +1,49 @@ +package com.wtd; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +/** + * @program: SpinLock + * @description: + * @author: Mr.Wang + * @create: 2023-10-29 08:20 + **/ +public class SpinLockDemo { + AtomicReference atomicReference= new AtomicReference<>(); + public void lock(){ + Thread thread = Thread.currentThread(); + System.out.println(Thread.currentThread().getName()+"\t"+"进来了"); + while (!atomicReference.compareAndSet(null, thread)) { + + } + } + public void unLock(){ + Thread thread = Thread.currentThread(); + atomicReference.compareAndSet(thread,null); + System.out.println(Thread.currentThread().getName()+"\t"+"走了"); + } + + public static void main(String[] args) { + SpinLockDemo spinLockDemo = new SpinLockDemo(); + new Thread(()->{ + spinLockDemo.lock(); + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException e) { + e.printStackTrace(); + } + spinLockDemo.unLock(); + },"A").start(); + try{ + TimeUnit.MILLISECONDS.sleep(500); + }catch (InterruptedException e){ + e.printStackTrace(); + } + new Thread(()->{ + spinLockDemo.lock(); + + spinLockDemo.unLock(); + },"B").start(); + } +}