Good design for threads keeps running to track if there is a pending request

I have a thread pool with 10 threads. I want these 10 threads to keep running in order to keep track of if new requests are pending. Is this a good design? If so, how can I get them to work? The reason is right now, they did the job once and then they finished. If not, which design is better? Any way we have one thread to keep doing work and the other to live while there is still another request awaiting? Thanks to

+3


source to share


2 answers


Is this a good design?

In principle, no.

There is never a good design for overriding the functionality provided by classes in the Java SE Standard Class Library.



In this case, you should use a ExecutorService

restricted threadpool implementation. It may also be advisable to use a limited work queue to avoid problems if the request rate might exceed your system's ability to handle them.

(If your request queue is not limited, or if the boundary is too large, you may run into problems such as the request timeout before your system starts processing the request. If your system cannot detect this, then it wastes resources in processing requests. answers that won't be used. A bounded queue with relatively little binding ... or even SynchronousQueue

... is recommended if you have to deal with overloading.)

0


source


Threads run () should run in a loop, something like this:

class Executor extends Thread {
    Runnable task;

    public void run() {
        for (;;) {
            task.run();
            synchronized (this) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }

      



queue based version:

public void run() {
    for (;;) {
        try {
            Runnable task = queue.take();
            task.run();
        } catch (InterruptedException e) {
        }
    }
}

      

+2


source







All Articles