CrazyEngineers
  • Explain the concept of message passing in java with an example

    Updated: Oct 26, 2024
    Views: 2.0K
    Can someone please explain me the concept of message passing in java with an example?What does object invoking methods on other object means?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Whats In Name

    MemberMay 13, 2013

    -bump
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorMay 13, 2013

    Whats In Name
    -bump
    Tagging #-Link-Snipped-# , #-Link-Snipped-# to look into this. I miss #-Link-Snipped-# as well.
    Are you sure? This action cannot be undone.
    Cancel
  • avii

    MemberMay 13, 2013

    Whats In Name
    Can someone please explain me the concept of message passing in java with an example?
    Tried <a href="https://en.wikipedia.org/wiki/Message_passing" target="_blank" rel="nofollow noopener noreferrer">Message Passing</a> ?
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberMay 14, 2013

    I guess you are asking how two thread communicates each other. that is via message passing.
    This is generally done with shared memory (class variable, static variable).
    If not going inside complexity of multhithreading (It's not complex until you get what it is), use of Getter and Setter can be seen as message passing.
    Suppose you have a class of Dog, with two method setBark and getBark, what you do is you create a object of Dog--> set the value in setBark(String how) and then send the object as parameter to recieverObject where you can fetch using getBark()
    .
    If you go to Multithreading example, you can take producer-consumer problem, Unless producer doen't set the value consumer can't get it. For more you can see this#-Link-Snipped-#
    Hope you got what you are asking for😀
    Are you sure? This action cannot be undone.
    Cancel
  • avii

    MemberMay 14, 2013

    Hey wait, shared memory is one of IPC, but in message passing how shared memory is used ? #-Link-Snipped-#

    In fact, in distributed systems, due to complexities of sharing memory we go with message passing for IPC. Please correct me if I am wrong.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberMay 14, 2013

    avii
    Hey wait, shared memory is one of IPC, but in message passing how shared memory is used ? #-Link-Snipped-#
    In fact, in distributed systems, due to complexities of sharing memory we go with message passing for IPC. Please correct me if I am wrong.
    My bad for creating confusion with concept of shared memory.
    Yes, you are right. What I meant to say is solving problems related to message-passing to object.

    For example I took of Producer-consumer problem, By algorithm concept both producer and consumer share a common memory and first procedure get lock of shared memory to write things and then consumer checks for lock and when it find unlock then read that memory (Correct me, for this also been a while).
    Now, can we implement this in java,
    Yes by threading and using setter and getter.(I given the link in previous post)
    We can implement message passing in java in following ways...
    • In a class, class variable and/or static variable can be used for all object.
    • In a program we use object passing through parameters.
    • In same/across jvm we can use serialized object for message passing
    • In one jvm, between two application system/server properties can be used. like in Tomcat sever we use Application Context to pass object from one application to other.
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberMay 14, 2013

    Class dog {

    String br;

    Void Setbarking(String B)

    {

    B=br;

    }

    Void Getbarking()

    {

    s.o.p(B);

    }}



    Class dogbark

    {

    Pulic static void main(String args[])

    dog d=new dog()

    d.B=”bark”;

    }}
    Are you sure? This action cannot be undone.
    Cancel
  • Whats In Name

    MemberMay 15, 2013

    sulochana anand
    Class dog {

    String br;

    Void Setbarking(String B)

    {

    B=br;

    }

    Void Getbarking()

    {

    s.o.p(B);

    }}



    Class dogbark

    {

    Pulic static void main(String args[])

    dog d=new dog()

    d.B=”bark”;

    }}
    Is this an example of message passing?
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberMay 15, 2013

    yes as ianoop explained
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register