9

What is message passing in Java? If you could, please provide an example.

UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33
Pavalesh
  • 247
  • 2
  • 4
  • 12
  • 9
    message passing can refer to several different things, ranging from simply calling method on objects, to communication between threads, to distributed communication between different computers - which type are you wondering about? – TofuBeer Jul 11 '10 at 09:01
  • Do you mean "message sending" vs. "method invocation"? In which case the answer http://stackoverflow.com/questions/2852381/calling-a-method-or-sending-a-message-in-objective-c is a good one. – ewernli Jul 11 '10 at 11:13

3 Answers3

18

Message Passing In Java

  • When a thread sends a message (an object) to another thread.

  • Used for thread communication and synchronization in environments where the threads do not have shared memory Hence the threads cannot share semaphores or monitors and cannot use shared variables to communicate. Message passing can still be used, of course, in a shared memory platform.

  • Messages are sent through a channel with an operation like send(channel, message) and received from a channel with an operation like receive(channel, message). Messages can be passed synchronously, meaning the sender blocks until the received does a receive and the receiver blocks until the sender does a send. Since the sender and receiver are at specific known points in their code at a known specific instant of time, synchronous message passing is also called a simple rendezvous with a one-way flow of information from the sender to the receiver. An example is a chess game agent. The agents can process messages synchronously, since they'll be handshaking throughout the entire game.

  • In asynchronous message passing, the sender does not block. If there is not a receiver waiting to receive the message, the message is queued or buffered. The receiver still blocks if there is no queued or buffered message when a receive is executed.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
  • **1)** For interaction between threads, When to prefer `send()` / `receive()` over `get()` /`put()` on shared object ? **2)** You said message passing can be used in shared memory platform, but [answer](https://stackoverflow.com/a/1853317/3317808) says, *workers cannot modify each other's data.* – overexchange Nov 12 '17 at 04:23
12

Classic interaction between two threads: a Producer and a Consumer.

import java.util.Vector; 

class Producer extends Thread { 
    static final int MAXQUEUE = 5; 
    private Vector messages = new Vector(); 

    public void run() { 
        try { 
            while ( true ) { 
                putMessage(); 
                sleep( 1000 ); 
            } 
        }  
        catch( InterruptedException e ) { } 
    } 

    private synchronized void putMessage() 
        throws InterruptedException { 

        while ( messages.size() == MAXQUEUE ) 
            wait(); 
        messages.addElement( new java.util.Date().toString() ); 
        notify(); 
    } 

    // Called by Consumer 
    public synchronized String getMessage() 
        throws InterruptedException { 
        notify(); 
        while ( messages.size() == 0 ) 
            wait(); 
        String message = (String)messages.firstElement(); 
        messages.removeElement( message ); 
        return message; 
    } 
} 

class Consumer extends Thread { 
    Producer producer; 

    Consumer(Producer p) { 
        producer = p; 
    } 

    public void run() { 
        try { 
            while ( true ) { 
                String message = producer.getMessage(); 
                System.out.println("Got message: " + message); 
                sleep( 2000 ); 
            } 
        }  
        catch( InterruptedException e ) { } 
    } 

    public static void main(String args[]) { 
        Producer producer = new Producer(); 
        producer.start(); 
        new Consumer( producer ).start(); 
    } 
} 
T.T.T.
  • 33,367
  • 47
  • 130
  • 168
  • But still you share a buffer to access thru synchronization. How different is this from `get()` and `put()` operation? – overexchange Nov 12 '17 at 04:15
-1

Your question is a bit vague, but I guess you might be referring to the Java Message Service API? If so, Wikipedia can tell you all about it: http://en.wikipedia.org/wiki/Java_Message_Service

But if you're talking about more "generic" message passing, then I suggest you have a look at the link ewernli posted!

Nim
  • 631
  • 6
  • 12