Skip to content

Commit

Permalink
Making modifications to allow reuse of threads to handle client conne…
Browse files Browse the repository at this point in the history
…ctions

Each request is handled by spawning a new thread and finished by
disconnecting. Modifying threads to be handled by a cached thread pool
to minimize the number of thread creation.
  • Loading branch information
Burcu Dogan committed Feb 9, 2012
1 parent 1d08aba commit fd00a4b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/net/aib42/directserver/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

import net.aib42.directserver.CommandParser;
Expand All @@ -17,12 +19,14 @@ public class Server
private AtomicLong nextClientId;
private CommandParser commandParser;
private HashMap<Byte, Module> modules;
private ExecutorService executor;

public Server()
{
nextClientId = new AtomicLong();
commandParser = new CommandParser(this);
modules = new HashMap<Byte, Module>();
executor = Executors.newCachedThreadPool();
}

/**
Expand Down Expand Up @@ -60,7 +64,7 @@ public void handleClientConnect(SocketChannel clientChannel)
}

Thread clientThread = new Thread(client, "Client #" + client.getId());
clientThread.start();
executor.execute(clientThread);
}

/**
Expand Down

0 comments on commit fd00a4b

Please sign in to comment.