Home > database >  Spring Boot Queue Poll
Spring Boot Queue Poll

Time:06-09

I am using Spring Boot (server) to post an API request (Android, retrofit). Spring Boot supports multiple threads to post the API.

When I receive multiple API requests to the server at exactly the same time, I need them to run asynchronously, but Spring launches a new thread for each.

I have tried to use Queues and then to poll the object one by one, however the Queue is either polled at the same time, or if I make the thread sleep, all of the threads sleep for exactly that amount of time, then every object is retrieved concurrently.

Can anyone advise how to poll slowly one by one. Please note, that the concurrency is needed for all other post request, but only this particular post request requires this delay.

CodePudding user response:

You can use thread synchronisation to protect your Queue from concurrent access. I've added a sample code below.

private static final Queue<T> queue = initQueue();

public void accessQueueA() {
    synchronized(queue) {
       // access queue;
    }
}
  • Related