How do I send a message to everyone

Question from GummyJon#4984:

Hey! I'm currently trying to make a custom communication client between me and my friends, and I'm coming across an issue. I have a server running which creates a different thread per person, but I want it to run a block of code along all threads, so everybody gets the same message. How can this be done?

For context, this is the code itself per thread

import java.net.*;
import java.io.*;

public class Handler extends Thread{

  private Socket socket = null;

  public Handler(Socket socket){
      super("Handler");
      this.socket = socket;
  }

  public void run() {
      try (
               PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
               BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
       ) {
           String inputLine;
           while ((inputLine = in.readLine()) != null) {
               System.out.println(inputLine);
               out.println(inputLine);
           }
       } catch (
               IOException e) {
           System.out.println("Exception caught");
       }
   }
}

Have each thread have a method of communication. In practice this can mean each that each thread as its own queue it reads off of and the "producer" writes the same message to the queue of each "consumer".

Here is a decent rundown of your options, but ArrayBlockingQueue is probably good enoughhere is a decent rundown of your options, but ArrayBlockingQueue is probably good enough, so:

record Message(String contents) {}

import java.net.*;
import java.io.*;
import java.util.concurrent.ArrayBlockingQueue;

public class Handler extends Thread{
    private Socket socket = null;
    private final ArrayBlockingQueue<Message> queue;
    
    public Handler(ArrayBlockingQueue<Message> queue, Socket socket){
        super("Handler");
        this.queue = queue;
        this.socket = socket;
    }
    
    public void run() {
        while (true) {
            Message msg;
            try {
                msg = this.queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

And then push things into the queue from another thread. Though please don't extend thread, just implement Runnable.

record Message(String contents) {}

import java.net.*;
import java.io.*;
import java.util.concurrent.ArrayBlockingQueue;

public class Handler implements Runnable {
    private Socket socket = null;
    private final ArrayBlockingQueue<Message> queue;
    
    public Handler(ArrayBlockingQueue<Message> queue, Socket socket){
        super("Handler");
        this.queue = queue;
        this.socket = socket;
    }
    
    @Override
    public void run() {
        while (true) {
            Message msg;
            try {
                msg = this.queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

<- Index