Java 多播接收数据和并行处理

发布于 2024-12-20 08:13:12 字数 838 浏览 2 评论 0 原文

我正在用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-12-27 08:13:12

如果您希望程序同时执行操作,则需要使用线程。1

最简单的方法(不涉及匿名类等)可能是将其中一个或另一个分开将您的流程放入一个实现 的单独类中可运行。例如,创建一个 Counter 类,如下所示:

public class Counter implements Runnable {
    public void run ( ) {
        int i = 0;
        while(true) {
            System.out.print(i);
            i++;
        }
    }
}

然后,在其他代码之前,您将创建一个新的 Thread 如下:

Counter counter = new Counter( );
Thread thread = new Thread(counter);
thread.start( );
...
// The rest of your code goes here...

这个额外的线程将与原始线程并行执行,原始线程现已移动处理您的套接字代码。当然,如果您愿意,您也可以为此启动一个新线程,但这有点多余,因为您已经有两个线程(新线程和原始线程)。

这实际上只是触及了线程的表面。它的内容远比我真正能给出的答案要多得多,所以我强烈建议阅读链接的文档,阅读 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 a Counter class as follows:

public class Counter implements Runnable {
    public void run ( ) {
        int i = 0;
        while(true) {
            System.out.print(i);
            i++;
        }
    }
}

Then, just before your other code, you would create a new Thread as follows:

Counter counter = new Counter( );
Thread thread = new Thread(counter);
thread.start( );
...
// The rest of your code goes here...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文