Message-processing-task should stop processing a NEW message when it detects a client-login-task. However, the message-processor should complete the task it was processing before pausing. This basically means the client-login task should give the message processor a breathing space (await itself) before it can continue. So the scenario is this
1) message-processing-task is processing messages from a queue (one at a time)
2) It detects a client-login in the middle of processing a message
3) message-processing-task should complete processing the message before it waits for the client-login-task to complete. This is where the client-login-task must wait.
4) client-login-task complete and signals message-processing-task
5) message-processing-task goes about its business.
My question is , are there any ready made synchronizers between these two thread which are executing different paths and yet must wait on each other? My understanding is Cyclic Barrier, Semaphore, CountDownLatch synchronize between threads which are in the same paths of execution.
EDIT- There is a single message-processing thread. However, there can be multiple login threads.
I solution which I have in mind is to use a Reentrant lock. So what happens is before processing each message, a lock is acquired and message-processor checks if there are any client login in progress. An AtomicInteger tells me the number of login requests in progress. If there are more than one login requests in progress, the notification-processor awaits on the lock condition. The condition on which notification-processor is signalled to resume its work is that the AtomicInteger count has to come down to 0. The only caveat with the solution is if the message is being processed and the login request comes in the middle of that, then the login thread does not wait. That is where I will need another lock on the client-login which must be released when the message-processor has processed the message. This makes the solution far too complex and I would like to avoid this unnecessary complexity. Any suggestions appreciated.
Not sure I picked this up correctly, but I would use a ThreadPoolExecutor
with single thread, passing a PriorityBlockingQueue
in it. All your tasks will go to that queue, the login tasks will have higher priority so they will all be processed before any message-processing
tasks.
Also if message-processing-task
is in progress, it will complete before the client-login-tasks
kicks in. Would that be what you need?