Java 什么是消息传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3222375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is message passing?
提问by Pavalesh
What is message passing in Java? If you could, please provide an example.
Java中的消息传递是什么?如果可以,请举例说明。
采纳答案by Romain Hippeau
Message Passing In Java
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.
当一个线程向另一个线程发送消息(一个对象)时。
用于线程没有共享内存的环境中的线程通信和同步,因此线程不能共享信号量或监视器,也不能使用共享变量进行通信。当然,在共享内存平台中仍然可以使用消息传递。
消息通过通道发送,使用类似 send(channel, message) 的操作,并从通道接收,使用如 receive(channel, message) 的操作。消息可以同步传递,这意味着发送方阻塞直到接收方进行接收,而接收方阻塞直到发送方进行发送。由于发送方和接收方在已知的特定时刻处于其代码中的特定已知点,因此同步消息传递也称为简单的集合点,从发送方到接收方的信息单向流。一个例子是国际象棋游戏代理。代理可以同步处理消息,因为他们将在整个游戏中握手。
在异步消息传递中,发送方不会阻塞。如果没有接收者等待接收消息,则消息被排队或缓冲。如果在执行接收时没有排队或缓冲的消息,接收器仍然会阻塞。
回答by Nim
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
您的问题有点含糊,但我猜您可能指的是 Java Message Service API?如果是这样,维基百科可以告诉你一切: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!
但是,如果您正在谈论更多“通用”消息传递,那么我建议您查看 ewernli 发布的链接!
回答by T.T.T.
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();
}
}