Simultaneous Processing of java.nio.channels.Selector

I work with java.nio.channels.Selector, and I would like to create a separate thread for each selected Key, which is ready for read / write / accept, but I want to make sure that the same socket is never processed by two different threads at the same time. What would be the best way to do this? I thought to cancel each selectedKey before creating a thread that will process its socket and reregister the socket to a selector as soon as the thread completes its life, but i / m is not sure how efficient it will be.

+3
source share
2 answers

There's a really nice Doug Lea presentation on the scalable Java I / O that I followed when creating my server. I use the following approach:

In my "reactor" there is one input / output stream that performs only input / output operations (and very simple decoding / encoding); It simply translates between bytes and message objects, and then passes the incoming message objects to the thread pool to handle business logic. I would highly recommend this approach - If the I / O stream is not saturated, there is no need for more than one I / O stream , and I would suggest that most of the I / O bottlenecks are due to the fact that another processing place in this thread .

, - , " ", , , , . , , , SelectionKey.

+9

, . , Thread . , . java.util.concurrent.Executors . http://java.sun.com/docs/books/tutorial/essential/concurrency/pools.html.

, , : . , , , .

, , , . , ().

, :

  • . klutzy, .

  • , . a Set, , , . , . .

+1

Source: https://habr.com/ru/post/1727760/


All Articles