Is it safe to pass the same ExecutorService instance to multiple NioServerSocketChannelFactory in Netty
Question
I have a tcp server running at port xxxx and flash policy server(again tcp) at port 843. Is it ok if I pass the same ExecutorService instance while creating the server bootstrap's for both servers? Will it lead to data corruption?
ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("TCP-Server-Boss")
ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("TCP-Server-Worker")
serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(boss,worker))
Now I want to pass the same boss
and worker
to the flash policy servers serverBootstrap
. Is this a valid usage?
Solution
The ExecutorService
returned by newCachedThreadPool()
is not guaranteed to be threadsafe anywhere in the documentation. So it would be the correct action to create two new ExecutorService
s.
Im quite sure the instances returned are threadsafe regardless of the documentation, but I would still make two new ExecutorService
s with different names to ease debugging and readability.