title: Creating the ServerSocket in a separate thread?
body: <p>I have a problem with using a <code>ServerSocket</code> in my application.</p>
  
  <p>I'm creating the <code>ServerSocket</code> in the constructor of my application. The constructor of the socket calls the <code>accept()</code> method to wait for a client to connect.</p>
  
  <p>The problem is that the <code>accept()</code> method is freezing my whole application until a client connects. So I would like to ask if there's an alternative to creating the whole <code>ServerSocket</code> in a separate thread, that the constructor of the <code>ServerSocket</code> and its <code>accept()</code> method is called beside my main application?</p>
  
  <p>Edit:</p>
  
  <p>Thanks to Olivier for the advice, putting the .accept into a runnable and creating a threadpool to handle the clientconnections.</p>
  
  <p>Thats my code right now:</p>
  
  <pre><code>  public void start(){
  
        final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);
  
        Runnable serverTask = new Runnable() {
            @Override
            public void run() {
  
                try {
                    serverSocket = new ServerSocket(port);
  
                    while (true) {
                        Socket clientSocket = serverSocket.accept();
                        objectout = new ObjectOutputStream(clientSocket.getOutputStream());
                        clientProcessingPool.submit(new ClientTask(clientSocket,objectout)); 
                    }
                } catch (IOException e) {
                    System.err.println("Accept failed.");
                }
  
            }
        };
  </code></pre>
  
  <p>Everythings running fine! Thanks!</p>