What is message passing and why it is used?
Message Passing in terms of computers is communication between processes. It is a form of communication used in object-oriented programming as well as parallel programming. Message passing in Java is like sending an object i.e. message from one thread to another thread. It is used when threads do not have shared memory and are unable to share monitors or semaphores or any other shared variables to communicate. Suppose we consider an example of producer and consumer, likewise what producer will produce, the consumer will be able to consume that only. We mostly use Queue to implement communication between threads.
In the example explained below, we will be using vector(queue) to store the messages, 7 at a time and after that producer will wait for the consumer until the queue is empty.
In Producer there are two synchronized methods putMessage() which will call form run() method of Producer and add message in Vector whereas getMessage() extracts the message from the queue for the consumer.
Using message passing simplifies the producer-consumer problem as they don’t have to reference each other directly but only communicate via a queue.
Got message: Thu May 09 06:57:53 UTC 2019 Got message: Thu May 09 06:57:54 UTC 2019 Got message: Thu May 09 06:57:55 UTC 2019 Got message: Thu May 09 06:57:56 UTC 2019 Got message: Thu May 09 06:57:57 UTC 2019 Got message: Thu May 09 06:57:58 UTC 2019 Got message: Thu May 09 06:57:59 UTC 2019 Got message: Thu May 09 06:58:00 UTC 2019
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Differences between Dynamic Binding and Message Passing in Java
- Difference between Shared Memory Model and Message Passing Model in IPC
- Message Dialogs in Java (GUI)
- Passing and Returning Objects in Java
- Parameter Passing Techniques in Java with Examples
- Java Swing | Creating a Toast Message
- Java Swing | Creating Custom Message Dialogs
- IPC using Message Queues
- How to pop an alert message box using PHP ?
- Passing Reference to a Pointer in C++
- getParameter() - Passing data from client to JSP
- How to send message on WhatsApp in Android
- Difference between Pipes and Message Queues
- Perl | Passing Complex Parameters to a Subroutine
- What is HMAC(Hash based Message Authentication Code)?
- How to display popup message when logged out user try to vote ?
- How to create popup message using Alerter Library in android
- Form required attribute with a custom validation message in HTML5
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SiddharthPandey4