{"id":78331,"date":"2025-07-27T00:06:39","date_gmt":"2025-07-27T00:06:39","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78331"},"modified":"2025-07-27T00:06:40","modified_gmt":"2025-07-27T00:06:40","slug":"demonstrating-wait-and-notify-in-a-simple-producer-consumer-problem","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78331","title":{"rendered":"Demonstrating wait() and notify() in a simple producer-consumer problem&#8221;"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Check on the methods on: <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Object.html\">https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Object.html<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Understanding <code>wait()<\/code>, <code>notify()<\/code>, and <code>notifyAll()<\/code> in Java<\/h3>\n\n\n\n<p>In <strong>Java<\/strong> and <strong>Java EE<\/strong>, the methods <code>wait()<\/code>, <code>notify()<\/code>, and <code>notifyAll()<\/code> are defined in the <strong><code>java.lang.Object<\/code><\/strong> class. They are <strong>not<\/strong> part of classes like <code>Thread<\/code> or interfaces such as <code>Runnable<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">How They Work:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>These methods are part of Java\u2019s <strong>built-in synchronization<\/strong> system, often referred to as the <strong>monitor mechanism<\/strong>.<\/li>\n\n\n\n<li>Every Java object has a monitor (or lock) that can be controlled through <code>synchronized<\/code> blocks or methods.<\/li>\n\n\n\n<li>When a thread enters a synchronized section, it gains exclusive access to the object&#8217;s monitor and can use:\n<ul class=\"wp-block-list\">\n<li><code>wait()<\/code> \u2013 makes the current thread pause and release the monitor until another thread calls <code>notify()<\/code> or <code>notifyAll()<\/code> on the same object.<\/li>\n\n\n\n<li><code>notify()<\/code> \u2013 wakes up one thread that&#8217;s waiting for the monitor.<\/li>\n\n\n\n<li><code>notifyAll()<\/code> \u2013 wakes up <strong>all<\/strong> threads waiting on that object.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SharedBuffer {\n    private int data;\n    private boolean hasData = false;\n\n    \/\/ Producer puts data into the buffer\n    public synchronized void produce(int value) throws InterruptedException {\n        while (hasData) {\n            wait(); \/\/ Wait until the buffer is empty\n        }\n        data = value;\n        hasData = true;\n        System.out.println(\"Produced: \" + data);\n        notify(); \/\/ Notify the consumer\n    }\n\n    \/\/ Consumer retrieves data from the buffer\n    public synchronized int consume() throws InterruptedException {\n        while (!hasData) {\n            wait(); \/\/ Wait until data is available\n        }\n        hasData = false;\n        System.out.println(\"Consumed: \" + data);\n        notify(); \/\/ Notify the producer\n        return data;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Threads using the Buffer<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ProducerConsumerExample {\n    public static void main(String&#91;] args) {\n        SharedBuffer buffer = new SharedBuffer();\n\n        \/\/ Producer Thread\n        Thread producer = new Thread(() -> {\n            try {\n                for (int i = 1; i &lt;= 5; i++) {\n                    buffer.produce(i);\n                    Thread.sleep(500); \/\/ simulate work\n                }\n            } catch (InterruptedException e) {\n                Thread.currentThread().interrupt();\n            }\n        });\n\n        \/\/ Consumer Thread\n        Thread consumer = new Thread(() -> {\n            try {\n                for (int i = 1; i &lt;= 5; i++) {\n                    buffer.consume();\n                    Thread.sleep(1000); \/\/ simulate work\n                }\n            } catch (InterruptedException e) {\n                Thread.currentThread().interrupt();\n            }\n        });\n\n        producer.start();\n        consumer.start();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>This code is copyright-free for your use<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The code I provided is original and <strong>generated by ChatGPT<\/strong>.<\/li>\n\n\n\n<li>It is not copied from any copyrighted source.<\/li>\n\n\n\n<li>You are <strong>free to use, modify, and publish it<\/strong>, including for <strong>personal, educational, or commercial<\/strong> purposes.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Check on the methods on: https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Object.html Understanding wait(), notify(), and notifyAll() in Java In Java and Java EE, the methods wait(), notify(), and notifyAll() are defined in the java.lang.Object class. They are not part of classes like Thread or interfaces such as Runnable. How They Work: Threads using the Buffer This code is copyright-free for &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=78331\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1975],"tags":[],"class_list":["post-78331","post","type-post","status-publish","format-standard","hentry","category-anything-java","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":22130,"url":"http:\/\/bangla.sitestree.com\/?p=22130","url_meta":{"origin":78331,"position":0},"title":"SCJP: Topics and Resources : will be continued #SCJP","author":"Sayed","date":"March 10, 2021","format":false,"excerpt":"SCJP topics and related resources are provided. I have skimed through the resources at least one time.Garbage Collection Test area:Given a code example, recognize the point at which an object becomes eligible for garbage collection, determine what is and is not guaranteed by the garbage collection system, and recognize the\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10210,"url":"http:\/\/bangla.sitestree.com\/?p=10210","url_meta":{"origin":78331,"position":1},"title":"StoppableThread.java A template to place a thread in a RUN, WAIT, or STOP state.","author":"","date":"August 25, 2015","format":false,"excerpt":"\/** A template to control the state of a thread through setting \u00a0*\u00a0 an internal flag. public class StoppableThread extends Thread { \u00a0\u00a0 public static final int STOP\u00a0\u00a0\u00a0 = 0; \u00a0\u00a0 public static final int RUN\u00a0\u00a0\u00a0\u00a0 = 1; \u00a0\u00a0 public static final int WAIT\u00a0\u00a0\u00a0 = 2; \u00a0\u00a0 private int state\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26858,"url":"http:\/\/bangla.sitestree.com\/?p=26858","url_meta":{"origin":78331,"position":2},"title":"# StoppableThread.java A template to place a thread in a RUN, WAIT, or STOP state. #Programming Code Examples #Java\/J2EE\/J2ME #Java Threads","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"\/** A template to control the state of a thread through setting * an internal flag. public class StoppableThread extends Thread { public static final int STOP = 0; public static final int RUN = 1; public static final int WAIT = 2; private int state = RUN; \/** Public\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65850,"url":"http:\/\/bangla.sitestree.com\/?p=65850","url_meta":{"origin":78331,"position":3},"title":"SCJP: Rules #Java Short Notes #SCJP","author":"Sayed","date":"July 16, 2021","format":false,"excerpt":"A class's superclasses don't have to implement Serializable in order to be serialized if a superclass doesn't implement Serializable then it's constructor will run during deserialization A transient variable's state is lost during serialization, but a volatile variable's state is not lost Java:Volatile variable Transient Variable NumberFormat, Calendar, DateFormat are\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10536,"url":"http:\/\/bangla.sitestree.com\/?p=10536","url_meta":{"origin":78331,"position":4},"title":"Code examples for interfaces","author":"","date":"August 29, 2015","format":false,"excerpt":"**************************** Code examples for interfaces: \u00a0\u00a0\u00a0 * Class1.java implements Interface1.java \u00a0\u00a0\u00a0 * Abstract Class2.java implements Interface1.java and Interface2.java \u00a0\u00a0\u00a0 * Class3.java extends abstract class Class2.java \u00a0\u00a0\u00a0 * Interface3.java extends Interface1.java and Interface2.java *************************** ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Class1.java ~~~~~~~~~~~~~~~~~~~~~~~~~~~ \/\/ This class is not abstract, so it must provide \/\/ implementations of method1\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65844,"url":"http:\/\/bangla.sitestree.com\/?p=65844","url_meta":{"origin":78331,"position":5},"title":"Java : SCJP: Important Resources #Java Short Notes #SCJP","author":"Sayed","date":"July 16, 2021","format":false,"excerpt":"How to use generics to avoid runtime errors. More Generics Class casting in Java: How to avoid runtime exception - ClassCastException: overloading, overriding, variable and method hiding Java HotSpot virtual machine What Java Technology can do? Offers from Java Technology Java:Common Problems (and Their Solutions) Benefits of OOP: Modularity, Information-hiding,\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78331"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78331\/revisions"}],"predecessor-version":[{"id":78332,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78331\/revisions\/78332"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78331"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}