I need to create an application that will receive data over the network and pass it on to other classes for processing. I decided to use the observer template for this, but I ran into a problem. Multiple connections can arrive at the same time, so for each one I create a new instance of the class, accepting the connection and processing the data. But how then is it right to attach observers to the server class. To use something in an additional thread, the variable must be static or final. The final is not suitable, because for each connection a new server instance. And static confuses me, I do not quite understand how it works.

Below is the code, tell me, please, how to organize it correctly, so that during operation no garbage got out.

This is the main class

public class Main { static Server server = new Server(); public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { int i = 0; // счётчик подключений ServerSocket socket = new ServerSocket(3013); while(true)// слушаем порт { server = new Server(i, socket.accept()); i++; } } catch(Exception e){ System.out.println("init: " + e); } } }).start(); ConcreteObserver observer = new ConcreteObserver(server); } } 

This is a server class.

 public class Server extends Thread implements Observable { Socket socket; int num; List<String> string = null; private static List<Observer> observers; public Server() { observers = new ArrayList<Observer>(); } public Server(int num, Socket s) { this.num = num; this.socket = s; start(); } public void run() { // doing something notifyObservers(); } public void addObserver(Observer o) { if (o != null) { observers.add(o); } } public void deleteObserver(Observer o) { if (o != null) { observers.remove(o); } } public void notifyObservers() { for (Observer observer : observers) { observer.update(listMettUnits); } } } 

And the observer class

 public class ComputeAll implements Observer { private Server server = null; public ComputeAll(Server server) { this.server = server; server.addObserver(this); } public void update(List<String> list) { // doing something } } 
  • How and when do you register new observers? Can a new observer register when the server is already up and accepting connections? Then he should register with every existing connection handler? Or just for new handlers, and let existing ones continue to work as they are? - Tagir Valeev
  • New observers are registered immediately after the launch of the application and that's it. A stream with a server socket is immediately created and started, and then observers are added. Theoretically, a situation may arise when the application is launched and the connection is wedged between the registration of two observers, but the probability is not great. Maybe we should register observers before starting the stream with the server? - Gvenihvivar
  • And one more question. What happens with the link to the server when one is already created and receives and connects the second. The application is working. But what exactly is happening at this moment? Observers are attached to the link. How notify from the first connection reaches the observers, if the server link is already attached to the new instance. - Gvenihvivar
  • Observers are in a static list, and he is one for all instances of the server. - Tagir Valeev
  • For sure! Understand. It turns out the whole decision will live. Is there anything else that must be taken into account so that there are no problems? - Gvenihvivar

1 answer 1

I have a few comments on this code.

  1. for observers it is not recommended to use LinkedList , it is better to use a thread-safe implementation, for example java.util.concurrent.CopyOnWriteArrayList ;
  2. it is preferable to register observers before starting the server.
  3. Fields in class Server and ComputeAll I would make final
  4. Running a thread in its constructor is not a good idea, and in general, creating threads explicitly is not good, use java.util.concurrent.ExecutorService as an option.
  5. And as for non-multithreading, the observers are static, which is probably not quite true.