{"id":4015,"date":"2021-11-28T13:10:52","date_gmt":"2021-11-28T04:10:52","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4015"},"modified":"2021-11-28T13:19:12","modified_gmt":"2021-11-28T04:19:12","slug":"java-synchronized","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4015","title":{"rendered":"java synchronized"},"content":{"rendered":"<h1>java synchronized<\/h1>\n<p><a href=\"https:\/\/coding-start.tistory.com\/68\">\ucc38\uc870<\/a><\/p>\n<h2>\uc815\uc758<\/h2>\n<p>Multi Thread \uc0c1\ud669\uc5d0\uc11c \ud2b9\uc815 \ub370\uc774\ud0c0\uc758 \ub3d9\uae30\ud654\ub97c \ubcf4\uc7a5\ud558\ub294 \ud0a4\uc6cc\ub4dc\uc774\ub2e4.<\/p>\n<p>\ub370\uc774\ud0c0\uc758 \uc548\uc815\uc131\uacfc \uc2e0\ub8b0\uc131\uc744 \ubcf4\uc7a5\ud558\ub294 \ud0a4\uc6cc\ub4dc\uc774\uc9c0\ub9cc,<br \/>\n\ud558\ub098\uc758 Thread \ub9cc \ub370\uc774\ud0c0\uc5d0 \uc811\uadfc \uac00\ub2a5\ud558\uace0, \ub2e4\ub978 Thread \ub294 \ub370\uc774\ud0c0\uc5d0 \uc811\uadfc\ud560 \uc218 \uc5c6\ub2e4.<\/p>\n<p>\uc989, \ub370\uc774\ud0c0\uc5d0 \uc811\uadfc\ud558\ub294 \ub2e4\ub978 \ubaa8\ub4e0 Thread \ub294 \uc2e4\ud589\uc774 \uba48\ucd94\uac8c \ub41c\ub2e4.<\/p>\n<h2>\uc0ac\uc6a9\ubc95<\/h2>\n<h3>\uba54\uc11c\ub4dc\uc5d0\uc11c \uc0ac\uc6a9<\/h3>\n<p>\ud558\ub098\uc758 Thread \ub9cc \uba54\uc18c\ub4dc\ub97c \ud638\ucd9c\ud560 \uc218 \uc788\ub2e4.<\/p>\n<pre><code class=\"language-java\">public class TestThread {\n\n    public static void main(String[] args) {\n\n        TestThread thread = new TestThread();\n\n        System.out.println(&quot;Test start!&quot;);\n\n        new Thread(() -&gt; {\n            for (int i = 1; i &lt;= 5; i++) {\n                thread.call(&quot;Thread A&quot;);\n            }\n        }).start();\n\n        new Thread(() -&gt; {\n            for (int i = 1; i &lt;= 5; i++) {\n                thread.call(&quot;Thread B&quot;);\n            }\n        }).start();\n\n        System.out.println(&quot;Test end!&quot;);\n    }\n\n    public synchronized void call(String who) {\n\n        System.out.println(who + &quot; started.&quot;);\n        try {\n            System.out.println(who);\n            long sleep = (long) (Math.random() * 100);\n            Thread.sleep(sleep);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        System.out.println(who + &quot; ended.&quot;);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">Test start!\nTest end!\nThread A started.\nThread A\nThread A ended.\nThread B started.\nThread B\nThread B ended.\nThread A started.\nThread A\nThread A ended.\nThread B started.\nThread B\nThread B ended.\nThread A started.\nThread A\nThread A ended.\nThread B started.\nThread B\nThread B ended.\nThread B started.\nThread B\nThread B ended.\nThread A started.\nThread A\nThread A ended.\nThread B started.\nThread B\nThread B ended.\nThread A started.\nThread A\nThread A ended.<\/code><\/pre>\n<h3>\ubcc0\uc218\uc5d0 \uc0ac\uc6a9<\/h3>\n<pre><code class=\"language-java\">public class TestThread {\n\n    final Integer COUNT = 0;\n\n    public static void main(String[] args) {\n\n        TestThread thread = new TestThread();\n\n        System.out.println(&quot;Test start!&quot;);\n\n        new Thread(() -&gt; {\n            for (int i = 1; i &lt;= 5; i++) {\n                thread.call(&quot;Thread A&quot;);\n            }\n        }).start();\n\n        new Thread(() -&gt; {\n            for (int i = 1; i &lt;= 5; i++) {\n                thread.call(&quot;Thread B&quot;);\n            }\n        }).start();\n\n        System.out.println(&quot;Test end!&quot;);\n    }\n\n    public void call(String who) {\n\n        System.out.println(who + &quot; started.&quot;);\n        try {\n            synchronized(COUNT) {\n                System.out.println(who + &quot; COUNT started.&quot;);\n                System.out.println(who);\n                long sleep = (long) (Math.random() * 100);\n                Thread.sleep(sleep);\n                System.out.println(who + &quot; COUNT ended.&quot;);\n            }\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        System.out.println(who + &quot; ended.&quot;);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-bash\">Test start!\nTest end!\nThread A started.\nThread B started.\nThread A COUNT started.\nThread A\nThread A COUNT ended.\nThread B COUNT started.\nThread B\nThread A ended.\nThread A started.\nThread B COUNT ended.\nThread B ended.\nThread B started.\nThread B COUNT started.\nThread B\nThread B COUNT ended.\nThread A COUNT started.\nThread A\nThread B ended.\nThread B started.\nThread A COUNT ended.\nThread B COUNT started.\nThread B\nThread A ended.\nThread A started.\nThread B COUNT ended.\nThread A COUNT started.\nThread A\nThread B ended.\nThread B started.\nThread A COUNT ended.\nThread B COUNT started.\nThread B\nThread A ended.\nThread A started.\nThread B COUNT ended.\nThread A COUNT started.\nThread A\nThread B ended.\nThread B started.\nThread A COUNT ended.\nThread B COUNT started.\nThread B\nThread A ended.\nThread A started.\nThread B COUNT ended.\nThread B ended.\nThread A COUNT started.\nThread A\nThread A COUNT ended.\nThread A ended.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>java synchronized \ucc38\uc870 \uc815\uc758 Multi Thread \uc0c1\ud669\uc5d0\uc11c \ud2b9\uc815 \ub370\uc774\ud0c0\uc758 \ub3d9\uae30\ud654\ub97c \ubcf4\uc7a5\ud558\ub294 \ud0a4\uc6cc\ub4dc\uc774\ub2e4. \ub370\uc774\ud0c0\uc758 \uc548\uc815\uc131\uacfc \uc2e0\ub8b0\uc131\uc744 \ubcf4\uc7a5\ud558\ub294 \ud0a4\uc6cc\ub4dc\uc774\uc9c0\ub9cc, \ud558\ub098\uc758 Thread \ub9cc \ub370\uc774\ud0c0\uc5d0 \uc811\uadfc \uac00\ub2a5\ud558\uace0, \ub2e4\ub978 Thread \ub294 \ub370\uc774\ud0c0\uc5d0 \uc811\uadfc\ud560 \uc218 \uc5c6\ub2e4. \uc989, \ub370\uc774\ud0c0\uc5d0 \uc811\uadfc\ud558\ub294 \ub2e4\ub978 \ubaa8\ub4e0 Thread \ub294 \uc2e4\ud589\uc774 \uba48\ucd94\uac8c \ub41c\ub2e4. \uc0ac\uc6a9\ubc95 \uba54\uc11c\ub4dc\uc5d0\uc11c \uc0ac\uc6a9 \ud558\ub098\uc758 Thread \ub9cc \uba54\uc18c\ub4dc\ub97c \ud638\ucd9c\ud560 \uc218 \uc788\ub2e4. public class TestThread { public\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4015\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-4015","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4015"}],"version-history":[{"count":2,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4015\/revisions"}],"predecessor-version":[{"id":4017,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4015\/revisions\/4017"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}