Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:45:49+00:00 2026-05-27T10:45:49+00:00

I am writing a class in Java to simplify the process of connecting to,

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:45:50+00:00Added an answer on May 27, 2026 at 10:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a class in Java to simplify the process of connecting to,
I have some experience writing web applications in Java for class projects. My first
I am writing a StringOutputStream class in Java because I need the pre-Base64-encoded data
I am writing a Scala class to inherit from a Java class, and I
Currently I am writing a program for an introductory Java class. I have two
I am writing a custom java class to grab objects from a Coherence grid.
hello I have some question about writing class in Java, why this one is
I have stumbled upon an annoyance as I was writing a Java class; I
I am writing a class in Java which is used to greatly simplify the
I'm writing a class in Java to represent a Graph data structure. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.