title: java singleton thread safe
body: <p>Ok, here is my problem. </p>
  
  <p>I need to create a socket program that can handle multiple connection from my client apps (lets call it apps1). I handle this using thread (so each connection was thrown into a new thread)</p>
  
  <p>The problem is I can accept request from all open connection but when I want to send a response , I must send it through the latest connection only. So if I have 3 connections (con1,con2,con3) I can accept request from con1, con2 and con3 but I must send the response through con3 (assuming con3 is the latest connection)</p>
  
  <p>I thought of using a singleton, with a PrintWriter parameter. So everytime there is a new connection ,they call the singleton and update the parameter and when I want to send the response, I get the PrintWriter first before sending.</p>
  
  <p>Here is my Singleton class :</p>
  
  <pre><code>public class Singleton {
  
  private static final Singleton instance = new Singleton();
  
  PrintWriter  out;
  
  public static Singleton getInstance() {
      return instance;
  }
  public Singleton ()
  {
      if (instance != null) {
          throw new IllegalStateException("Already instantiated");
      }
  }
  
  public PrintWriter getPrintWriter ()
  {
      return this.out;
  }
  public void updatePrintWriter (PrintWriter  out){
      this.out = out;
  }
  }
  </code></pre>
  
  <p>This is my main program :</p>
  
  <pre><code>public class SocketAccept{
  private ServerSocket mainSocket;
  private Socket clientSocket;
  
      public SocketAccept (int portNumber) {
          Singleton s = Singleton.getInstance();
          do {
          try {
              mainSocket = new ServerSocket(portNumber);
              clientSocket = mainSocket.accept();
              s.updatePrintWriter(new PrintWriter(clientSocket.getOutputStream(), true));
              ClientThread (clientSocket);
          } catch (IOException ex) {
              Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
          }
          }while (true);//need to change this into thread pool or connection pool    
      } 
  }
  </code></pre>
  
  <p>and this is my thread that handle socket :</p>
  
  <pre><code>public class ClientThread extends Thread {
  
      private Socket cs;
      Singleton s = Singleton.getInstance();
      PrintWriter out;
  
      private String read(Socket sc) {
          String request = "";
          //read request here
          return request;
      }
  
      private String process(String request) {
          String response = "";
          //process request here
          return response;
      }
  
      public ClientThread(Socket clientSocket) {
          this.cs = clientSocket;
      }
  
      @Override
      public void run() {
          String requestMsg = "";
          String responseMsg = "";
          do {
              requestMsg = read(cs);// read the message
  
              if (requestMsg.equalsIgnoreCase("SHUTDOWN")) {
                  break;
              }
              responseMsg = process(requestMsg);
              out = s.getPrintWriter();
              out.write(responseMsg);
          } while (true);
      }
  }
  </code></pre>
  
  <p>Do I did it right? Or it is impossible to do it with singleton?</p>
  
  <p>Thanks for the help.</p>