jchambers / java-otp

A one-time password (HOTP/TOTP) library for Java
MIT License
455 stars 122 forks source link

Use synchronization instead of ThreadLocals for thread safety #12

Closed jchambers closed 5 years ago

jchambers commented 5 years ago

The ThreadLocal-based approach introduced in 5e625e7 works, but I'm concerned that it's making things too complicated and introduces too much baggage for casual use cases. As an alternative, this approach just uses synchronization to manage access to stateful resources.

I like this approach because:

  1. It maintains thread-safety.
  2. The performance cost in single-threaded cases is negligible.
  3. Callers can still create one HOTP/TOTP generator per thread if they're really concerned about throughput.