From f0244ef106e25396c110eb50416496f0fe5ba5e2 Mon Sep 17 00:00:00 2001 From: daadaadaah Date: Sun, 19 Feb 2023 17:57:35 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[volatile=20=EC=B6=94=EA=B0=80=20=EC=A0=84]?= =?UTF-8?q?=20=EC=95=84=EB=9E=98=EC=B2=98=EB=9F=BC=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=EB=90=98=EA=B3=A0=20sample=20=EC=93=B0=EB=A0=88=EB=93=9C?= =?UTF-8?q?=EB=8A=94=20=EC=A2=85=EB=A3=8C=EB=90=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > 출력 결과 --------------------- VolatileSample : run() start RunVolatitle : sleep() end RunVolatitle : Set value is completed ! --- practice/volatile1/RunVolatile.java | 24 ++++++++++++++++++++++++ practice/volatile1/VolatileSample.java | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 practice/volatile1/RunVolatile.java create mode 100644 practice/volatile1/VolatileSample.java diff --git a/practice/volatile1/RunVolatile.java b/practice/volatile1/RunVolatile.java new file mode 100644 index 000000000..d72cad490 --- /dev/null +++ b/practice/volatile1/RunVolatile.java @@ -0,0 +1,24 @@ +package volatile1; + +public class RunVolatile { + public static void main(String[] args) { + RunVolatile runVolatile = new RunVolatile(); + runVolatile.runVolatitleSample(); + } + + public void runVolatitleSample() { + VolatileSample sample = new VolatileSample(); + sample.start(); + + try { + Thread.sleep(1000); + } catch(Exception e) { + e.printStackTrace(); + } + + System.out.println("RunVolatitle : sleep() end "); + sample.setDouble(-1); + System.out.println("RunVolatitle : Set value is completed !"); + + } +} diff --git a/practice/volatile1/VolatileSample.java b/practice/volatile1/VolatileSample.java new file mode 100644 index 000000000..e88445537 --- /dev/null +++ b/practice/volatile1/VolatileSample.java @@ -0,0 +1,26 @@ +package volatile1; + +public class VolatileSample extends Thread { + + private double instanceVariable = 0; + + void setDouble(double value) { + this.instanceVariable = value; + } + + public void run() { + System.out.println("--------------------- VolatileSample : run() start "); + + try { + while(instanceVariable == 0) { + Thread.sleep(1); + } + } catch(Exception e) { + e.printStackTrace(); + } + + System.out.println("--------------------- VolatileSample : instanceVariable : "+instanceVariable); + + System.out.println("--------------------- VolatileSample : run() end "); + } +} From 09c4cb2d48419669c763e0229e0914faeaa9f4a9 Mon Sep 17 00:00:00 2001 From: daadaadaah Date: Sun, 19 Feb 2023 17:58:32 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[volatile=20=EC=B6=94=EA=B0=80]=20=EC=95=84?= =?UTF-8?q?=EB=9E=98=EC=B2=98=EB=9F=BC=20=EC=B6=9C=EB=A0=A5=EB=90=98?= =?UTF-8?q?=EA=B3=A0=20sample=20=EC=93=B0=EB=A0=88=EB=93=9C=EB=8A=94=20?= =?UTF-8?q?=EC=A2=85=EB=A3=8C=EB=90=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > 출력 결과 --------------------- VolatileSample : run() start RunVolatitle : sleep() end RunVolatitle : Set value is completed ! --------------------- VolatileSample : instanceVariable : -1.0 --------------------- VolatileSample : run() end --- practice/volatile1/VolatileSample.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/practice/volatile1/VolatileSample.java b/practice/volatile1/VolatileSample.java index e88445537..2d563c424 100644 --- a/practice/volatile1/VolatileSample.java +++ b/practice/volatile1/VolatileSample.java @@ -2,7 +2,7 @@ public class VolatileSample extends Thread { - private double instanceVariable = 0; + private volatile double instanceVariable = 0; void setDouble(double value) { this.instanceVariable = value; @@ -11,13 +11,7 @@ void setDouble(double value) { public void run() { System.out.println("--------------------- VolatileSample : run() start "); - try { - while(instanceVariable == 0) { - Thread.sleep(1); - } - } catch(Exception e) { - e.printStackTrace(); - } + while(instanceVariable == 0); System.out.println("--------------------- VolatileSample : instanceVariable : "+instanceVariable); From 7e042e97a5870d992d4b40b3ff082bdbd1cd1611 Mon Sep 17 00:00:00 2001 From: daadaadaah Date: Sun, 19 Feb 2023 17:59:17 +0900 Subject: [PATCH 3/4] =?UTF-8?q?volatile=EB=A1=9C=20=EC=84=A0=EC=96=B8?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EC=95=8A=EC=95=84=EB=8F=84=20=EC=93=B0?= =?UTF-8?q?=EB=A0=88=EB=93=9C=EA=B0=80=20=EB=A9=88=EC=B6=98=EB=8B=A4.=20?= =?UTF-8?q?=EC=99=9C=20=EA=B7=B8=EB=9F=AC=EB=8A=94=20=EA=B1=B8=EA=B9=8C=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > 출력 결과 --------------------- VolatileSample : run() start RunVolatitle : sleep() end RunVolatitle : Set value is completed ! --------------------- VolatileSample : instanceVariable : -1.0 --------------------- VolatileSample : run() end --- practice/volatile1/VolatileSample.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/practice/volatile1/VolatileSample.java b/practice/volatile1/VolatileSample.java index 2d563c424..e88445537 100644 --- a/practice/volatile1/VolatileSample.java +++ b/practice/volatile1/VolatileSample.java @@ -2,7 +2,7 @@ public class VolatileSample extends Thread { - private volatile double instanceVariable = 0; + private double instanceVariable = 0; void setDouble(double value) { this.instanceVariable = value; @@ -11,7 +11,13 @@ void setDouble(double value) { public void run() { System.out.println("--------------------- VolatileSample : run() start "); - while(instanceVariable == 0); + try { + while(instanceVariable == 0) { + Thread.sleep(1); + } + } catch(Exception e) { + e.printStackTrace(); + } System.out.println("--------------------- VolatileSample : instanceVariable : "+instanceVariable); From fc52b19abda83e5985bfeb509c5b7d297a0b7366 Mon Sep 17 00:00:00 2001 From: daadaadaah Date: Sun, 19 Feb 2023 18:01:27 +0900 Subject: [PATCH 4/4] =?UTF-8?q?while=20=EB=AC=B8=EC=97=90=20print=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EB=84=A3=EC=96=B4=EB=8F=84=20=EC=93=B0?= =?UTF-8?q?=EB=A0=88=EB=93=9C=EB=8A=94=20=EC=A2=85=EB=A3=8C=EB=90=9C?= =?UTF-8?q?=EB=8B=A4.=20=EC=99=9C=20=EA=B7=B8=EB=9F=AC=EB=8A=94=20?= =?UTF-8?q?=EA=B1=B8=EA=B9=8C=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/volatile1/VolatileSample.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/practice/volatile1/VolatileSample.java b/practice/volatile1/VolatileSample.java index e88445537..0da64e7b4 100644 --- a/practice/volatile1/VolatileSample.java +++ b/practice/volatile1/VolatileSample.java @@ -11,12 +11,8 @@ void setDouble(double value) { public void run() { System.out.println("--------------------- VolatileSample : run() start "); - try { - while(instanceVariable == 0) { - Thread.sleep(1); - } - } catch(Exception e) { - e.printStackTrace(); + while(instanceVariable == 0) { + System.out.println("--------------------- VolatileSample : while ing... "); } System.out.println("--------------------- VolatileSample : instanceVariable : "+instanceVariable);