- Yeah definitely use an ExecutorService. Also if you really intend to be running threads for months if not years, you might want to brush up your skills first. Clearly there are a lot of things still unclear to you.
– Kayaman
Dec 12, 2017 at 7:57
You've pretty much written some sort of executor service. Why do you have a timeout on your queue.poll? You could eliminate the whole queue/run/timeout bits and use an executor. Also, your shutdown doesn't shut anything down. I know you've left out some code, but your threads certainly aren't going to respect a shutdown. Plus, you have try, catch generic throwable, which calls a method, that does a try catch generic throwable. Definitely look into an ExecutorService.
– matt
Dec 12, 2017 at 8:18
Add a comment
2 Answers
Sorted by:
4
Answering your question, if you have threads which has the same lifetime of the application, in my opinion it doesn't matter if you are using a Thread or Executer service (which is again using Threads underneath) as long as you manage the thread's life cycle properly.
From design point of view your application falls in to software category what we called a "middleware". Generally a middleware application should be efficient as well as scalable, both which are essential qualities of such server yet you have ignored both. Your application's threads run busy-wait loops per thread keeping the CPU busy at all time. Even when the incoming load is very low this keeps happening. Which is a not good quality to have for such application.
Alternatively, I'm proposing you to use a ThreadPool implementation such as ThreadPoolExecutor which already have solved what you are trying to accomplish here. ThreadPoolExecutor leverages the functionality of a BlockingQueue if all initially fired up threads are busy at the moment. also it can stop threads if the load is low and fire up again if wanted. I have coded the structure of the design I'm proposing. Take a look at the following code. I assumed that Client is not thread-safe so I'm constructing a Client per thread. If your