I am trying to implement a simple program which allows to join a Multicast group on a virtual IP address and listen packets that are sent to this IP (that is why I created the class `ThreadGroup`).

My code is :

```
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class MulticastTest {
    private Set<Thread> threads;

    public MulticastTest(){
        threads = new HashSet<Thread>();
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        MulticastTest test = new MulticastTest();
        test.joinGroup("dogg",test.createAdr(),32445);

    }

    private void joinGroup(String name, String adr, int port){
          try {
              MulticastSocket multi = new MulticastSocket(port);
              multi.joinGroup(InetAddress.getByName(adr));
              ThreadGroup newThread = new ThreadGroup(multi);
              threads.add(newThread);
              newThread.start();
              System.out.println("Congrats you joined the group "+name+".");
          }catch (NumberFormatException | IOException e) {
              e.printStackTrace();
        }
      }

      private String createAdr(){
          Random r = new Random();
          return r.nextInt(16)+224 + "." + r.nextInt(256) + "." + (r.nextInt(255)+1) + "." + r.nextInt(56);
      }

    /**
     * Thread to receive datagram that are sent to the group
     */

    class ThreadGroup extends Thread {
        MulticastSocket multiSocket;

        public ThreadGroup(MulticastSocket m) throws IOException{
            multiSocket = m;
            start();
        }

        public void run() {
            DatagramPacket message;
            byte[] contMessage;
            String texte;

            while(true) {
                contMessage = new byte[1024];
                message = new DatagramPacket(contMessage, contMessage.length);
                try {
                    multiSocket.receive(message);
                    texte = (new DataInputStream(new ByteArrayInputStream(contMessage))).readUTF();
                    System.out.println(texte);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

}

```

So in my main, I try to connect to a random Multicast `InetAddress` and wait packets. But when I run my program, I get :

```
Exception in thread "main" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:705)
    at MulticastTest.joinGroup(MulticastTest.java:35)
    at MulticastTest.main(MulticastTest.java:25)

```

Could somebody explain to me what I am doing wrong ?