初始化
commit
7eb159fca7
|
@ -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
|
|
@ -0,0 +1,10 @@
|
|||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Zeppelin 忽略的文件
|
||||
/ZeppelinRemoteNotebooks/
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.tong</groupId>
|
||||
<artifactId>SpinLockDemo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,51 @@
|
|||
package com.tong;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author: tongCheng
|
||||
* @Package: SpinLockDemo
|
||||
* @ClassName: SpinLockDemo
|
||||
* @date: 2023-10-28 13:46
|
||||
* 实现一个自旋锁,复习CAS思想
|
||||
* 自旋锁的好处:循环比较获取没有类似wait的阻塞
|
||||
* 通过CAS操作完成自旋锁,A线程先进来调用myLock方法自己持有锁5秒钟,B随后进来后
|
||||
* 发现当前线程持有锁,所有只能通过自旋等待,知道A释放锁后B随后抢到
|
||||
*/
|
||||
public class SpinLockDemo {
|
||||
|
||||
AtomicReference<Thread> atomicReference = new AtomicReference<>();
|
||||
|
||||
public void lock() {
|
||||
Thread thread = Thread.currentThread();
|
||||
System.out.println(Thread.currentThread().getName()+"\t"+"----come in");
|
||||
while (!atomicReference.compareAndSet(null, thread)) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void unLock() {
|
||||
Thread thread = Thread.currentThread();
|
||||
atomicReference.compareAndSet(thread, null);
|
||||
System.out.println(Thread.currentThread().getName()+"\t"+"----task over,unLock...");
|
||||
}
|
||||
|
||||
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();
|
||||
//暂停500毫秒,线程A先于B启动
|
||||
try { TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }
|
||||
|
||||
new Thread(() -> {
|
||||
spinLockDemo.lock();
|
||||
|
||||
spinLockDemo.unLock();
|
||||
}, "B").start();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue