Search code examples
javamultithreadingtimeoutthreadpoolblockingqueue

Java concurency - thread pool- multiple requests and users


I am trying to solve the task connected with Java multithreading. I have thought about using threads pool with blocking queue but I'm not sure whether it will be sufficient in my case.

the problem is that the number of requests must be limited

From what I understood thread pool should do fine in this example but what about the blocking queue? Don't you think there must be a better solution?

I have thought about using priority blocking queue to give the most active users lower priority but then the priority connected with waiting time (older requests should have bigger priority) should be also updated constantly- this will end in constant queue reorder.

Are there any defined solutions for such a problem?


Solution

  • How about using a Semaphore. Each incoming request would try to acquire a lock with timeout, then you can be sure that if acquiring a lock succeeds the request is instantly being processed. How's that sound?

    Semaphore documentation: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html

    I would use just one Semaphore, so the number of permits in the semaphore would effectively equal to max number of connections allowed in the system.

    As to the number of connection per user I would keep user connections counters in a ConcurrentHashMap in the semaphore implementation, then customise

    public boolean tryAcquire(long timeout, TimeUnit unit, Long userId)
        throws InterruptedException {
        AtomicLong connCount = userConnetctionMap.contains(userId) ? userConnetctionMap.get(userId) :  userConnetctionMap.put(userId, new AtomicLong(0));
        if (connCount.get() < MAX_USER_CONN_COUNT) {
            boolean locked = super.tryAcquire(timeout, unit);
            if (locked) {
                userConnetctionMap.get(userId).incrementAndGet();
                return true;
            }
        }
        return false;
    }
    

    to increment the count per user id if his number of conn is less than max allowed

    and custom action:

    releaseUSerConnection(userId)
    

    would decrement that counter