There are no significant differences in the two scenarios you posted, other than controlling the end of a stream in Scenario2; you always create a new thread for every incoming request. If you want to use ThreadPool, my advice is not to create it for each request, but to create it for each server and reuse threads. Sort of:
public class YourClass { //in init method or constructor ExecutorService executor = Executors....;// choose from newCachedThreadPool() or newFixedThreadPool(int nThreads) or some custom option int clientNumber = 0; ServerSocket listener = new ServerSocket(port); while(true) { executor.execute(new GPSService(listener.accept(), client++, serverUrl)); }
This will allow you to use the thread pool and control the number of threads used for your server. If you want to use Executor, this is the preferred way.
With a server pool, you need to decide how many threads are in the pool; you have different options, but you can start either with a fixed number or threads or with a pool that tries to use an idle thread, and if all threads are busy, it creates a new one ( newCachedThreadPool() ). The number of threads for distribution depends on many factors: the number of requests for approval and duration. The more your server code takes time, the more you need for extra thread. If your server-side code is very fast, there is a very good chance that the pool can reuse already allocated threads (since requests do not arrive completely at the same exact moment).
Say, for example, that you have 10 requests in a second, and each request lasts 0.2 seconds; if the request arrives at 0, 0.1, 0.2, 0.3, 0.4, 0.5, .. part two (for example, 23/06/2015 7: 16: 00: 00, 23/06 / 2015 7:16:00: 01, 06/23/2015 7: 16: 00: 02) you only need three threads, since a request starting with 0.3 can be executed by the thread on which the first request is executed (one of 0) etc. (The request at time 0.4 can reuse the stream used for the request, which came in 0.1). Ten requests are controlled by three threads.
I recommend that you (if you have not done so) read Java Concurrency in practice (completing the task - chapter 6); which is a great book on how to create a parallel Java application.