我正在用 Java 编写一个类来简化连接、加入、发送和接收多播组数据的过程。在我描述我的问题之前,请先看一下下面的示例,以便您了解我在做什么。
这是我正在做的事情的非常基本代码示例。请注意,它与我的实际代码完全不同,除了异常捕获、导入语句等之外...它只是显示了我的类如何利用 Java 多播功能的基本流程:
//Connect to the multicast host, and join the group
MulticastSocket msConn = new MulticastSocket(5540);
InetAddress netAddr = InetAddress.getByName("239.255.255.255");
msConn.joinGroup(netAddr);
//Preapre a datagram packet in which to place recieved data
byte buf[] = new byte[1024];
DatagramPacket pack = new DatagramPacket(buf, buf.length);
//Code halts here until data is recieved
msConn.recieve(pack);
请注意,在接收() 方法中,代码将暂停,直到多播器类收到数据包。我希望能够让 Java 持续侦听新数据,同时执行此代码:
int i = 0;
while(true) {
System.out.print(i);
i++;
}
这些过程可以并行完成吗?如果可以,您能否提供一个示例来说明如何做到这一点?如果没有,还有其他解决方法吗?
I am writing a class in Java to simplify the process of connecting to, joining, sending, and receiving data from a multicast group. Please have a look at the sample below before I describe my question, just so you have an idea of what I am doing.
Here is a very basic code sample of what I am doing. Note that it does not at all resemble my actual code, with the exception catching, import statements, etc... it simply shows the basic flow of my class in how it leverages Java's multicasting abilities:
//Connect to the multicast host, and join the group
MulticastSocket msConn = new MulticastSocket(5540);
InetAddress netAddr = InetAddress.getByName("239.255.255.255");
msConn.joinGroup(netAddr);
//Preapre a datagram packet in which to place recieved data
byte buf[] = new byte[1024];
DatagramPacket pack = new DatagramPacket(buf, buf.length);
//Code halts here until data is recieved
msConn.recieve(pack);
Note that, on the receive()
method, the code halts until the multicaster class receives a data packet. I would to be able to have Java continuously listen for new data, while also simultaneously executing this code:
int i = 0;
while(true) {
System.out.print(i);
i++;
}
Can these processes be done in parallel, and, if so, could you please provide an example of how to do this? If not, is there another work around?
发布评论
评论(1)
如果您希望程序同时执行操作,则需要使用线程。1
最简单的方法(不涉及匿名类等)可能是将其中一个或另一个分开将您的流程放入一个实现
的单独类中可运行
。例如,创建一个Counter
类,如下所示:然后,在其他代码之前,您将创建一个新的
Thread
如下:这个额外的线程将与原始线程并行执行,原始线程现已移动处理您的套接字代码。当然,如果您愿意,您也可以为此启动一个新线程,但这有点多余,因为您已经有两个线程(新线程和原始线程)。
这实际上只是触及了线程的表面。它的内容远比我真正能给出的答案要多得多,所以我强烈建议阅读链接的文档,阅读 thread 文章,并查找其他信息以更好地了解这些东西的工作原理。
1:如果您的线程在同一个处理器上运行,它们实际上不会同时运行,它们只是看起来如此。
If you want your program to be doing things simultaneously, you'll want to use threads.1
The easiest way (without going into things like anonymous classes, etc) is probably to split one or the other of your processes into a separate class that implements
Runnable
. For example, make aCounter
class as follows:Then, just before your other code, you would create a new
Thread
as follows:This extra thread will execute in parallel with the original thread, which has now moved onto handling your sockets code. You could of course start a new thread for that too if you wanted, but it is a little redundant since you already have two threads (your new one and the original).
This is really just scratching the surface of threading. There's far more to it than I can really give in just this answer, so I strongly suggest reading through the linked docs, reading through the thread article on Wikipedia, and finding other info to get a better idea of how this stuff works.
1: If your threads run on the same processor they won't really be running simultaneously, they'll just seem to be doing so.