{"id":10325,"date":"2020-10-01T19:07:43","date_gmt":"2020-10-01T10:07:43","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=10325"},"modified":"2020-10-05T07:10:34","modified_gmt":"2020-10-04T22:10:34","slug":"producer-consumer-%ed%8c%a8%ed%84%b4","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=10325","title":{"rendered":"Producer-Consumer \ud328\ud134"},"content":{"rendered":"<h4>\ud328\ud134 \uba85\uce6d<\/h4>\n<p>Producer-Consumer<\/p>\n<h4>\ud544\uc694\ud55c \uc0c1\ud669<\/h4>\n<p>\uc5b4\ub5a4 \ub370\uc774\ud130\uac00 \uc0dd\uc131\ub418\uba74, \uadf8 \uc0dd\uc131\ub41c \ub370\uc774\ud130\ub97c \ubc1b\uc544 \ucc98\ub9ac\ud558\ub294 \uc0c1\ud669\uc5d0\uc11c \uba40\ud2f0 \uc2a4\ub808\ub4dc\ub97c \ud65c\uc6a9\ud558\uc5ec \ucc98\ub9ac\ub7c9\uacfc \uc18d\ub3c4\ub97c \ub298\ub9ac\uace0\uc790\ud560 \uc801\uc6a9\ud560 \uc218 \uc788\ub294 \ud328\ud134\uc774\ub2e4. \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \uc2a4\ub808\ub4dc\uc640 \uc774\ub97c \ucc98\ub9ac\ud558\ub294 \uc2a4\ub808\ub4dc\ub97c \ubd84\ub9ac\ud560 \uc218 \uc788\uc73c\uba70 \uac01\uac01\uc740 1\uac1c \uc774\uc0c1\uc758 \uc2a4\ub808\ub4dc\ub85c \uad6c\uc131\ub420 \uc218 \uc788\ub2e4. \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \uc2a4\ub808\ub4dc\ub97c Producer, \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud558\ub294 \uc2a4\ub808\ub4dc\ub97c Consumer\uc774\ub2e4. Producer\ub294 \ucc98\ub9ac\ud558\uace0\uc790 \ud558\ub294 \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud574 \uc800\uc7a5\uc18c\uc5d0 \uc800\uc7a5\ud574 \ub450\uba74 Consumer\ub294 \uc774 \uc800\uc7a5\uc18c\uc5d0\uc11c \ub370\uc774\ud130\ub97c \ubc1b\uc544 \ucc98\ub9ac\ud55c\ub2e4. \uc5ec\uae30\uc11c \uc800\uc7a5\uc18c\ub294 Channel\uc774\ub77c\uace0 \ud55c\ub2e4. Channel\uc740 \ub370\uc774\ud130\ub97c \uc800\uc7a5\ud560 \uc218 \uc788\ub294 \ubb3c\ub9ac\uc801 \ud55c\uacc4\uac00 \uc788\uc73c\uba70, Producer\ub294 \uc774 \ubb3c\ub9ac\uc801 \ud55c\uacc4 \ub0b4\uc5d0\uc11c \ub370\uc774\ud130\ub97c \uc800\uc7a5\ud558\uace0 \uc800\uc7a5\ud560 \uc218 \uc788\ub294 \uacf5\uac04\uc774 \uc5c6\uc744 \uacbd\uc6b0 \ub300\uae30\ud55c\ub2e4. Consumer\ub294 Channel\uc5d0 \uc800\uc7a5\ub41c \ub370\uc774\ud130\ub97c \uac00\uc838\uc640 \ucc98\ub9ac\ud558\ub294\ub370, \ub9cc\uc57d Channel\uc5d0 \ub370\uc774\ud130\uac00 \uc5c6\uc744 \uacbd\uc6b0 \ub300\uae30\ud55c\ub2e4.<\/p>\n<h4>\uc608\uc81c \ucf54\ub4dc<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2020\/10\/Producer-Consumer.png\" alt=\"\" width=\"800\" class=\"aligncenter size-full wp-image-10326\" \/><\/p>\n<p>\uc704\uc758 \ud074\ub798\uc2a4 \ub2e4\uc774\uc5b4\uadf8\ub7a8\uc5d0\uc11c \uc5b8\uae09\ub41c \ud074\ub798\uc2a4\ub4e4\uc740 \uc55e\uc11c \uc124\uba85\ud55c \ub0b4\uc6a9\uacfc \uac19\ub2e4. \uba3c\uc800 \uc774 \ud074\ub798\uc2a4\ub4e4\uc744 \uc0ac\uc6a9\ud558\ub294 \uc608\uc81c \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic class Main {\r\n\tpublic static void main(String[] args) {\r\n\t\tChannel channel = new Channel(10);\r\n\t\t\r\n\t\tnew Producer(\"PRODUCER-1\", channel).start();\r\n\t\tnew Producer(\"PRODUCER-2\", channel).start();\r\n\t\t\r\n\t\tnew Consumer(\"CONSUMER-A\", channel).start();\r\n\t\tnew Consumer(\"CONSUMER-B\", channel).start();\r\n\t\tnew Consumer(\"CONSUMER-C\", channel).start();\r\n\t\tnew Consumer(\"CONSUMER-D\", channel).start();\r\n\t}\r\n}\r\n<\/pre>\n<p>Producer \uc2a4\ub808\ub4dc\ub294 2\uac1c, Consumer \uc2a4\ub808\ub4dc\ub294 4\uac1c\ub97c \uad6c\uc131\uc2dc\ud0a8\ub2e4. Channel\uc5d0 \uc800\uc7a5\ud560 \uc218 \uc788\ub294 \ub370\uc774\ud130\uc758 \ucd5c\ub300 \uc6a9\ub7c9\uc740 10\uc73c\ub85c \uc9c0\uc815\ud588\ub2e4. \uc774 \uc608\uc81c\uc5d0\uc11c Producer\uac00 \uc0dd\uc131\ud558\ub294 \ub370\uc774\ud130\ub294 \ubb38\uc790\uc5f4\uc774\uba70, Producer \ud074\ub798\uc2a4\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic class Producer extends Thread {\r\n\tprivate final Channel channel;\r\n\tprivate int number = 0;\r\n\t\r\n\tpublic Producer(String name, Channel channel) {\r\n\t\tsuper(name);\r\n\t\tthis.channel = channel;\r\n\t}\r\n\t\r\n\tpublic void run() {\r\n\t\ttry {\r\n\t\t\twhile(true) {\r\n\t\t\t\t\/\/Thread.sleep(200);\r\n\t\t\t\t\r\n\t\t\t\tString data = \"[DATA-\" + number + \"@\" + getName() + \"]\";\r\n\t\t\t\tchannel.put(data);\r\n\t\t\t\tnumber++;\r\n\t\t\t}\r\n\t\t} catch(InterruptedException e) {\r\n\t\t\t\/\/.\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\uc0dd\uc131\ub41c \ub370\uc774\ud130\ub97c \ubc1b\uc544 \ucc98\ub9ac\ud558\ub294 Consumer \ud074\ub798\uc2a4\ub294 \ub2e4\uc74c\uacfc \uac19\uc740\ub370, \ubc1b\uc740 \ubb38\uc790\uc5f4 \ub370\uc774\ud130\ub97c \uc870\ud569\ud55c \uc0c8\ub85c\uc6b4 \ubb38\uc790\uc5f4\uc744 \ucd9c\ub825\ud55c\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic class Consumer extends Thread {\r\n\tprivate final Channel channel;\r\n\t\r\n\tpublic Consumer(String name, Channel channel) {\r\n\t\tsuper(name);\r\n\t\tthis.channel = channel;\r\n\t}\r\n\t\r\n\tpublic void run() {\r\n\t\ttry {\r\n\t\t\twhile(true) {\r\n\t\t\t\tString data = channel.get();\r\n\t\t\t\t\r\n\t\t\t\t\/\/Thread.sleep(100);\r\n\t\t\t\tSystem.out.println(\"#: \" + getName() + \" consumes \" + data);\r\n\t\t\t}\r\n\t\t} catch(InterruptedException e) {\r\n\t\t\t\/\/.\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 Producer\uac00 \uc0dd\uc131\ud55c \ub370\uc774\ud130\ub97c \uc800\uc7a5\ud558\ub294 Channel \ud074\ub798\uc2a4\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\nimport java.util.LinkedList;\r\n\r\npublic class Channel {\r\n\tprivate final LinkedList&lt;String> dataBuffer = new LinkedList&lt;String>();\r\n\tprivate final int maxSize;\r\n\t\r\n\tpublic Channel(int maxSize) {\r\n\t\tthis.maxSize = maxSize;\r\n\t}\r\n\t\r\n\tpublic synchronized void put(String data) throws InterruptedException {\r\n\t\twhile(dataBuffer.size() >= maxSize) {\r\n\t\t\twait();\r\n\t\t}\r\n\t\t\r\n\t\tdataBuffer.addLast(data);\r\n\t\t\r\n\t\tnotifyAll();\r\n\t}\r\n\t\r\n\tpublic synchronized String get() throws InterruptedException {\r\n\t\twhile(dataBuffer.size() <= 0) {\r\n\t\t\twait();\r\n\t\t}\r\n\t\t\r\n\t\tString data = dataBuffer.pollFirst();\r\n\t\tnotifyAll();\r\n\t\t\r\n\t\t\/\/System.out.println(\"\\t\" + dataBuffer.size());\r\n\t\t\r\n\t\treturn data;\r\n\t}\r\n}\r\n<\/pre>\n<p>\uc2e4\ud589\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class='code'>\r\n#: CONSUMER-C consumes [DATA-58449@PRODUCER-1]\r\n#: CONSUMER-C consumes [DATA-59980@PRODUCER-2]\r\n#: CONSUMER-B consumes [DATA-58456@PRODUCER-1]\r\n#: CONSUMER-C consumes [DATA-59981@PRODUCER-2]\r\n#: CONSUMER-B consumes [DATA-59982@PRODUCER-2]\r\n#: CONSUMER-C consumes [DATA-59983@PRODUCER-2]\r\n\r\n.\r\n.\r\n.\r\n\r\n#: CONSUMER-B consumes [DATA-58476@PRODUCER-1]\r\n#: CONSUMER-A consumes [DATA-58477@PRODUCER-1]\r\n#: CONSUMER-B consumes [DATA-58478@PRODUCER-1]\r\n#: CONSUMER-A consumes [DATA-58479@PRODUCER-1]\r\n#: CONSUMER-B consumes [DATA-58480@PRODUCER-1]\r\n#: CONSUMER-A consumes [DATA-58481@PRODUCER-1]\r\n#: CONSUMER-C consumes [DATA-58475@PRODUCER-1]\r\n#: CONSUMER-C consumes [DATA-59988@PRODUCER-2]\r\n#: CONSUMER-D consumes [DATA-58472@PRODUCER-1]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud328\ud134 \uba85\uce6d Producer-Consumer \ud544\uc694\ud55c \uc0c1\ud669 \uc5b4\ub5a4 \ub370\uc774\ud130\uac00 \uc0dd\uc131\ub418\uba74, \uadf8 \uc0dd\uc131\ub41c \ub370\uc774\ud130\ub97c \ubc1b\uc544 \ucc98\ub9ac\ud558\ub294 \uc0c1\ud669\uc5d0\uc11c \uba40\ud2f0 \uc2a4\ub808\ub4dc\ub97c \ud65c\uc6a9\ud558\uc5ec \ucc98\ub9ac\ub7c9\uacfc \uc18d\ub3c4\ub97c \ub298\ub9ac\uace0\uc790\ud560 \uc801\uc6a9\ud560 \uc218 \uc788\ub294 \ud328\ud134\uc774\ub2e4. \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \uc2a4\ub808\ub4dc\uc640 \uc774\ub97c \ucc98\ub9ac\ud558\ub294 \uc2a4\ub808\ub4dc\ub97c \ubd84\ub9ac\ud560 \uc218 \uc788\uc73c\uba70 \uac01\uac01\uc740 1\uac1c \uc774\uc0c1\uc758 \uc2a4\ub808\ub4dc\ub85c \uad6c\uc131\ub420 \uc218 \uc788\ub2e4. \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \uc2a4\ub808\ub4dc\ub97c Producer, \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud558\ub294 \uc2a4\ub808\ub4dc\ub97c Consumer\uc774\ub2e4. Producer\ub294 \ucc98\ub9ac\ud558\uace0\uc790 \ud558\ub294 \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud574 \uc800\uc7a5\uc18c\uc5d0 \uc800\uc7a5\ud574 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=10325\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Producer-Consumer \ud328\ud134&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-10325","post","type-post","status-publish","format-standard","hentry","category-design"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10325"}],"version-history":[{"count":5,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10325\/revisions"}],"predecessor-version":[{"id":10339,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10325\/revisions\/10339"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10325"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}