From 146c7ff8a83f9d5d76168e1ae35d2cae26099e82 Mon Sep 17 00:00:00 2001 From: TangZhaoZhen <207525215@qq.com> Date: Sun, 29 Oct 2023 19:31:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 38 ++++++ .idea/.gitignore | 8 ++ .idea/encodings.xml | 7 ++ .idea/inspectionProfiles/Project_Default.xml | 5 + .idea/misc.xml | 27 ++++ .idea/uiDesigner.xml | 124 +++++++++++++++++++ .idea/vcs.xml | 6 + pom.xml | 17 +++ src/main/java/com/tzz/LockDemo.java | 59 +++++++++ 9 files changed, 291 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/tzz/LockDemo.java 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/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8d66637 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9e9713d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1b2fb5b --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.tzz + thread-lock + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/com/tzz/LockDemo.java b/src/main/java/com/tzz/LockDemo.java new file mode 100644 index 0000000..263a91d --- /dev/null +++ b/src/main/java/com/tzz/LockDemo.java @@ -0,0 +1,59 @@ +package com.tzz; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +/** + * @author : tangzhaozhen + * @createTime : 2023/10/28 16:47 + */ + +public class LockDemo { + + AtomicReference 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) { + LockDemo lockDemo = new LockDemo(); + + new Thread(()->{ + lockDemo.lock(); + try { + //暂停几秒中线程 + TimeUnit.SECONDS.sleep(5); + lockDemo.unLock(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + },"A").start(); + + try { + //暂停500毫秒,线程A先于线程B启动 + TimeUnit.MILLISECONDS.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + new Thread(()->{ + lockDemo.lock(); + lockDemo.unLock(); + },"B").start(); + + } +} \ No newline at end of file