From 771a34283fb8b82737e00cbf68d8839dc2d7c503 Mon Sep 17 00:00:00 2001 From: ex_kongxiang Date: Wed, 25 Oct 2023 11:42:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20synchronized=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ lock/SynchronizedLock.java | 38 ++++++++++++++++++++++++++ test/SynchronizedLockTest.java | 50 ++++++++++++++++++++++++++++++++++ thread/AThread.java | 13 +++++++++ thread/MyCallable.java | 11 ++++++++ thread/RunnableThread.java | 12 ++++++++ thread/ThreadCreate.java | 42 ++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 lock/SynchronizedLock.java create mode 100644 test/SynchronizedLockTest.java create mode 100644 thread/AThread.java create mode 100644 thread/MyCallable.java create mode 100644 thread/RunnableThread.java create mode 100644 thread/ThreadCreate.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e918cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +out/ +*.iml +*.xml \ No newline at end of file diff --git a/lock/SynchronizedLock.java b/lock/SynchronizedLock.java new file mode 100644 index 0000000..6eb36bf --- /dev/null +++ b/lock/SynchronizedLock.java @@ -0,0 +1,38 @@ +/** + * @author 孔翔 + * @since 2023-10-25 + * copyright for author : 孔翔 at 2023-10-25 + * java-study + */ +public class SynchronizedLock { + /** + * 共享变量 + */ + private int value; + + + public void update(int value) { + int sum = this.value + value; + try { + Thread.sleep(10); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + this.value = sum; + } + + + public synchronized void updateSafe(int value) { + int sum = this.value + value; + try { + Thread.sleep(10); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + this.value = sum; + } + + public int getValue() { + return value; + } +} diff --git a/test/SynchronizedLockTest.java b/test/SynchronizedLockTest.java new file mode 100644 index 0000000..522fd84 --- /dev/null +++ b/test/SynchronizedLockTest.java @@ -0,0 +1,50 @@ +import org.testng.annotations.Test; + +/** + * @author 孔翔 + * @since 2023-10-25 + * copyright for author : 孔翔 at 2023-10-25 + * java-study + */ +public class SynchronizedLockTest { + SynchronizedLock synchronizedLock = new SynchronizedLock(); + + @Test + public void testUpdate() { + new Thread(()->{ + int i = 0; + while (i++ < 10) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + synchronizedLock.updateSafe(100); + System.out.println(Thread.currentThread().getName()+":"+synchronizedLock.getValue()); + } + }).start(); + new Thread(()->{ + int i = 0; + while (i++ < 10) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + synchronizedLock.updateSafe(-80); + System.out.println(Thread.currentThread().getName()+":"+synchronizedLock.getValue()); + } + }).start(); + + while (true){ + try { + Thread.sleep(1000); + System.out.println("监控: "+synchronizedLock.getValue()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} + +//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme \ No newline at end of file diff --git a/thread/AThread.java b/thread/AThread.java new file mode 100644 index 0000000..b61205d --- /dev/null +++ b/thread/AThread.java @@ -0,0 +1,13 @@ +/** + * @author 孔翔 + * @since 2023-10-23 + * copyright for author : 孔翔 at 2023-10-23 + * java-study + */ +public class AThread extends Thread { + + @Override + public void run() { + System.out.println("hello : 我是A Thread"); + } +} diff --git a/thread/MyCallable.java b/thread/MyCallable.java new file mode 100644 index 0000000..45f7e5a --- /dev/null +++ b/thread/MyCallable.java @@ -0,0 +1,11 @@ +import java.util.concurrent.Callable; + +public class MyCallable implements Callable { + + @Override + public String call() throws Exception { + Thread.sleep(3000); + return "Hello, Callable!"; + } +} + diff --git a/thread/RunnableThread.java b/thread/RunnableThread.java new file mode 100644 index 0000000..6260614 --- /dev/null +++ b/thread/RunnableThread.java @@ -0,0 +1,12 @@ +/** + * @author 孔翔 + * @since 2023-10-23 + * copyright for author : 孔翔 at 2023-10-23 + * java-study + */ +public class RunnableThread implements Runnable{ + @Override + public void run() { + System.out.println("Runnable 实现: 线程 Runnable"); + } +} diff --git a/thread/ThreadCreate.java b/thread/ThreadCreate.java new file mode 100644 index 0000000..b2aa7c3 --- /dev/null +++ b/thread/ThreadCreate.java @@ -0,0 +1,42 @@ +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * @author 孔翔 + * @since 2023-10-23 + * copyright for author : 孔翔 at 2023-10-23 + * java-study + */ +public class ThreadCreate { + + public static void main(String[] args) { + // runnable + new Thread(new RunnableThread()).start(); + // extends Thread + AThread aThread = new AThread(); + aThread.start(); + + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future future = executor.submit(new MyCallable()); + try { + String result = future.get(); + System.out.println("Callable result: " + result); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } finally { + executor.shutdown(); + } + + + } + + + + + + +} -- GitLab