{"id":5351,"date":"2019-07-24T03:45:27","date_gmt":"2019-07-24T10:45:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5351"},"modified":"2019-07-24T03:45:54","modified_gmt":"2019-07-24T10:45:54","slug":"leetcode-1116-print-zero-even-odd","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/concurrent\/leetcode-1116-print-zero-even-odd\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1116. Print Zero Even Odd"},"content":{"rendered":"\n<p>Suppose you are given the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\">class ZeroEvenOdd {\n&nbsp; public ZeroEvenOdd(int n) { ... }&nbsp;     \/\/ constructor\n  public void zero(printNumber) { ... }  \/\/ only output 0's\n  public void even(printNumber) { ... }  \/\/ only output even numbers\n  public void odd(printNumber) { ... }   \/\/ only output odd numbers\n}\n<\/pre>\n\n\n\n<p>The same instance of&nbsp;<code>ZeroEvenOdd<\/code>&nbsp;will be passed to three different threads:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Thread A will call&nbsp;<code>zero()<\/code>&nbsp;which should only output 0&#8217;s.<\/li><li>Thread B will call&nbsp;<code>even()<\/code>&nbsp;which should only ouput even numbers.<\/li><li>Thread C will call&nbsp;<code>odd()<\/code>&nbsp;which should only output odd numbers.<\/li><\/ol>\n\n\n\n<p>Each of the thread is given a&nbsp;<code>printNumber<\/code>&nbsp;method to output&nbsp;an integer. Modify the given program to output the series&nbsp;<code>010203040506<\/code>&#8230; where the length of the series must be 2<em>n<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> n = 2\n<strong>Output:<\/strong> \"0102\"\n<strong>Explanation:<\/strong> There are three threads being fired asynchronously. One of them calls zero(), the other calls even(), and the last one calls odd(). \"0102\" is the correct output.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> n = 5\n<strong>Output:<\/strong> \"0102030405\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Mutex<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 40 ms \/ 9 MB\nclass ZeroEvenOdd {\nprivate:\n  int x;\n  int n;\n  mutex m_zero_;\n  mutex m_odd_;\n  mutex m_even_;\npublic:\n    ZeroEvenOdd(int n) {\n      this->n = n;\n      this->x = 1;\n      m_odd_.lock();\n      m_even_.lock();\n    }\n\n    \/\/ printNumber(x) outputs \"x\", where x is an integer.\n    void zero(function<void(int)> printNumber) {\n      for (int i = 0; i < n; ++i) {\n        m_zero_.lock();\n        printNumber(0);\n        if (i % 2 == 0) {\n          m_odd_.unlock();\n        } else {\n          m_even_.unlock();\n        }\n      }\n    }\n\n    void even(function<void(int)> printNumber) {\n      for (int i = 2; i <= n; i += 2) {\n        m_even_.lock();\n        printNumber(i);\n        m_zero_.unlock();\n      }\n    }\n\n    void odd(function<void(int)> printNumber) {\n      for (int i = 1; i <= n; i += 2) {\n        m_odd_.lock();\n        printNumber(i);\n        m_zero_.unlock();      \n      }\n    }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you are given the following code: class ZeroEvenOdd { &nbsp; public ZeroEvenOdd(int n) { &#8230; }&nbsp; \/\/ constructor public void zero(printNumber) { &#8230; }&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[478],"tags":[479,484,177,483,480],"class_list":["post-5351","post","type-post","status-publish","format-standard","hentry","category-concurrent","tag-concurrent","tag-lock","tag-medium","tag-mutex","tag-threading","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5351"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5351\/revisions"}],"predecessor-version":[{"id":5353,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5351\/revisions\/5353"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}