开发者

What is message passing?

What is message passing in Ja开发者_Go百科va? If you could, please provide an example.


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.


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(); 
    } 
} 


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜