chrisapotek
chrisapotek

Reputation: 6227

Does each SelectionKey (or SelectionKeyImpl) have a unique identifier (id) that I can use?

I need to have a unique ID for each of my SelectionKey. Can I use some internal id from the SelectionKeyImpl? I could use the FD for example, but I am not confident I will be always unique per SelectionKey.

Upvotes: 0

Views: 201

Answers (2)

Lolo
Lolo

Reputation: 4357

You could use your own ids (such as java.util.UUID) and attach them to the keys via SelectionKey.attach(Object) or when registering the channel with the Selector

Upvotes: 1

user207421
user207421

Reputation: 311023

There is no FD in a SelectionKey. There is an FD you can reach via the underlying Channel but a channel may have several SelectionKeys so that's no use to you. All you have is the SelectionKey's own identity.

Upvotes: 1

Related Questions