PrabhaT
PrabhaT

Reputation: 888

Handle the number of threads in an api

I have a api which may be used by multiple applications. I want to regulate my api so that I process only a fixed number of requess per application. Suppose app. X is giving some 30 requests and I hvae onl 20 allowed for this particular application to use my api. So I allow only 20 threads and mark others as waiting, similar things should be done for all apis.

What will be the best way to do this.

Upvotes: 0

Views: 141

Answers (2)

vivekv
vivekv

Reputation: 2298

What you are looking for is called a Semaphore. The class in java is

java.util.concurrent.Semaphore

Create a semaphore as in

mysem = new Semaphore(20);

Then in your API you do the following

void myAPI()  {
        mysem.acquire();
        //My API logic comes here
        finally {
           mysem.release();
        }
    }

Upvotes: 0

Alex Stybaev
Alex Stybaev

Reputation: 4693

How about looking at:

ExecutorService.newFixedThreadPool(int nThreads)

Executers on Java 1.5

Upvotes: 2

Related Questions