views:

71

answers:

1

I have a component that I wish to write and it's the kind of thing that feels like a common pattern. I was hoping to find the common name for the pattern if there is one, and examples of how to go about implementing it.

I have a service that queues requests and processes them one at a time. I have a number of client threads which make the requests. The key is that the calling threads must block until their own particular request is serviced.

E.g. if there are 10 threads, all making a request, then the 10th thread will block for longest while it waits for its request to make it to the front of the queue, and to be processed. In brief pseodocode, a call would be as simple as:

service.processMessage(myMessage); /* block whilst it enqueues, waits, */
                                   /* processes and returns */

I know what you're thinking - why bother having threads at all? Let's just say there are design constraints well outside my control.

Also, this should run on JavaME, which means an infuriating subset of real Java, and no swanky external libraries.

+3  A: 

If you do not have any requirements on the total ordering of handling requests (i.e., you don't mind arbitrarily mixing requests from different threads independent of the order they "arrive" in), you could simply make processMessage() synchronized, I guess.

ShiDoiSi
Wow. That's just obliterated a whole lot of over-engineering on my part. I might actually do this, despite the vague possibility of genuine concurrency in the future, because it's far too perfect to ignore. Thanks.
izb