{"id":748,"date":"2010-08-07T23:32:44","date_gmt":"2010-08-07T18:02:44","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=748"},"modified":"2010-08-07T23:32:44","modified_gmt":"2010-08-07T18:02:44","slug":"producer-consumer-problem","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/producer-consumer-problem\/","title":{"rendered":"Thread Synchronization"},"content":{"rendered":"<h3 style=\"text-align: left;\">Synchronization, Producer and Consumer Problem, wait(), notify() methods of the Thread<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Controller\n{\n  private int counter;\n\n  public void set(int i)\n  {\n      counter=i;\n  }\n\n  public int get()\n  {\n    return counter;\n  }\n}\n\nclass Producer extends Thread\n{\n  private Controller obj;\n\n public Producer(Controller c)\n {\n  Obj=c;\n }\n\npublic void run()\n{\n for(int i=0;I&amp;lt;5;i++ )\n {\n   obj.set(i);\n   System.out.println(\"put:\"\u009d+j);\n }\n}\n}\n\nclass Consumer extends Thread\n{\n  private\u00a0 Controller obj;\n\n  public Consumer(Controller c)\n  {\n    obj=c;\n  }\n\n  public void run()\n  {\n    for(int\u00a0 i=0;i&amp;lt;5;i++)\n    {\n        System.out.println(\"get:\"\u009d+obj.get());\n     }\n  }\n}\n\nclass TestSynchronization\n{\n public\u00a0 static void main(String args&#x5B;])\n {\n   Controller ctrl=new Controller;\n   Producer thread1=new producer(ctrl);\n   Consumer thread2=new Consumer(ctrl);\n   thread1.start();\n   thread2.start();\n  }\n}\n<\/pre>\n<p>O\/P:-<\/p>\n<p>put:0<br \/>\nget:0<br \/>\nget:0<br \/>\nget:0<br \/>\nget:0<br \/>\nget:0<br \/>\nput:1<br \/>\nput:2<br \/>\nput:3<br \/>\nput:4<\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>In above program the producer creates value faster or slower than consumer that means there is no synchronization &amp; this problem is known as producer_consumer problem.<\/p>\n<p>Solution Of Producer &amp; Customer Problem:-<\/p>\n<p><strong>Synchronized:<\/strong><\/p>\n<p>The synchronized block or synchronized method, allow only one thread at a time to execute.<\/p>\n<p><strong>Wait():<\/strong><\/p>\n<p>This method will cause the current thread to go into wait state until &amp; unless notify() or notifyAll() is not called.<\/p>\n<p><strong>notifyAll():<\/strong><\/p>\n<p>It will give signal to all the threads which are in state to continue.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Controller\n{\n  private int counter;\n  private Boolean available=false;\n  public synchronized int get()\n  {\n      while(available==false)\n     {\n      try\n        {\n          wait();\n        }\n        catch(Exception e)\n        {\n        }\n     }\n     notifyAll();\n    available=false;\n    return counter;\n  }\n\n public synchronized int set(int value)\n {\n   while(available==true)\n   {\n     try\n     {\n        wait();\n     }\n     catch(Exception e)\n     {\n     }\n     counter=value;\n     available=false;\n     notifyAll();\n    }\n  }\n}\n\nclass Producer extends Thread\n{\n  private Controller obj;\n  public Producer(Controller c)\n  {\n   Obj=c;\n  }\n\n  public void run()\n  {\n   for(int i=0;I&amp;lt;5;i++ )\n   {\n     obj.set(i);\n     System.out.println(\"put:\"\u009d+j);\n   }\n }\n}\n\nclass Consumer extends Thread\n{\n private \u00a0Controller obj;\n public Consumer(Controller c)\n {\n  obj=c;\n }\n public void run()\n {\n  for(int \u00a0i=0;i&amp;lt;5;i++)\n  {\n     System.out.println(\"get:\"\u009d+obj.get());\n  }\n }\n}\n\nclass TestSynchronization\n {\n  public \u00a0static void main(String args&#x5B;])\n  {\n     Controller ctrl=new Controller;\n     Producer thread1=new producer(ctrl);\n     Consumer thread2=new Consumer(ctrl);\n     thread1.start();\n     thread2.start();\n   }\n}\n<\/pre>\n<p>O\/P:<br \/>\nput:0<br \/>\nget:0<br \/>\nput:1<br \/>\nget:1<br \/>\nput:2<br \/>\nget:2<br \/>\nput:3<br \/>\nget:3<br \/>\nput:4<br \/>\nget:4<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Describes Synchronization, Producer and Consumer Problem, wait(), notify(), InterThread Communication<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[198],"class_list":["post-748","post","type-post","status-publish","format-standard","hentry","category-java","tag-thread"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":724,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/simple-threading-techniques\/","url_meta":{"origin":748,"position":0},"title":"Thread, Life Cycle of Thread","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"What is Thread? Life cycle of thread. Difference in start() and run() method of Thread. By Default how many thread runs?","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1825,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-thread-timertask\/","url_meta":{"origin":748,"position":1},"title":"Java Thread &#8211; Executor framework, Timer and TimerTask","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Tutorial and example of Executor framework, Timer and TimerTask over Thread","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Animation of ProgressBar in Swing Using TimerTask","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Animation-of-ProgressBar-Swing-Using-TimerTask.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":702,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-interfaceinheritance-abstract-class\/","url_meta":{"origin":748,"position":2},"title":"Interface, Inheritance and abstract class","author":"Jitendra","date":"August 6, 2010","format":false,"excerpt":"What is abstract class, interface and inheritance in java. Source code and example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Inheritance, Interface and abstract class","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Inheritance1-300x173.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1629,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/nested-class-and-its-necessity-with-example\/","url_meta":{"origin":748,"position":3},"title":"Nested Class and its necessity with example","author":"Jitendra","date":"March 11, 2011","format":false,"excerpt":"Example and Introduction of Nested classes in JAVA and there Types","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Nested Class in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Nested-Class-in-JAVA.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":979,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/invoke-method-using-reflection-in-java\/","url_meta":{"origin":748,"position":4},"title":"Invoke method using reflection in JAVA","author":"Jitendra","date":"September 6, 2010","format":false,"excerpt":"Invoke method using reflection in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-immutable-class-in-java\/","url_meta":{"origin":748,"position":5},"title":"How to create Immutable Class in Java","author":"Jitendra","date":"February 3, 2012","format":false,"excerpt":"creating the Immutable class in Java with simple source code example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=748"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/748\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}