Is there a way to ensure that threads are assigned to a specified set of objects?

后端 未结 5 726
旧时难觅i
旧时难觅i 2021-02-06 01:38

We are working on an application where a set of objects can be affected by receiving messages from 3 different sources. Each message (from any of the sources) has a single objec

5条回答
  •  甜味超标
    2021-02-06 02:21

    You should be able to provide a special BlockingQueue for ThreadPoolExecutor. The queue remembers which type of message is being processed by which thread, so that it can withhold all messages of the same type.

    MyQueue
    
        ownership relation of thread - msgType 
    
        take/poll()
    
            if current thread owns msg type X
                if there is a message of type X
                    return that message
                else
                    give up ownership
    
            // current thread does not own any message type
            if there is a messsage of type Y, Y is not owned by any thread
                current thread owns Y
                return that message
    
            // there's no message belonging to an unowned type
            wait then retry 
    

提交回复
热议问题