jeudi 13 juillet 2017

Design pattern for multithreaded java application

I have a backend that receives requests on a function

update(by_whom, payload);

I then process the request, and answer accordingly.

Every time a user sends a request, the message goes through several processing objects before an answer is produced.

Now, how would I go about handling each user in his very own thread? I don't understand how to dispatch events to tailor-made threads.

Example:

User 1 sends first request -> We make a ThreadUser1 and handle it there.

User 2 sends first request -> We make a ThreadUser2 and handle it there, doesn't matter if request1 from user1 hasn't finished, they can run together.

User 1 sends second request, the first one sent by him hasn't finished -> We dispatch this request on the threadUser1, and it will therefore wait for request1 to finish.

Basically, upon receiving a request, it should only be put in queue behind those sent by the same user, and be run concurrently with everything else.

A way I thought would work:

I thought about posting every request in a concurrent list from the main thread, and have threads check that list (while true), and pick the requests from the user which they belong to.

A few problems with this: - I'd need to check the whole list everytime for a request by the right user.

  • This assumes I already have a thread running for every possible user, which I don't, I only know the id of a user when he first texts.

  • Efficiency, potentially a huge amount of loops checking the same list over and over.

  • A thread should stop after all the requests from that user are finished.

On that last point: it's because I need to have a limit MAX whereby any requests received while we already have MAX threads running, with these new requests not belonging to any current user (aka running thread) are handled normally (queuing in the main thread, one after the other).

I really don't know how to go about this and I'd be very glad if anyone could point me in the right direction.

Aucun commentaire:

Enregistrer un commentaire