Please take a review at https://cr.openjdk.java.net/~weijun/8210476/webrev.00/
Before this fix, every PRNG::nextBytes calls all of CryptAcquireContext, CryptGenRandom, and CryptReleaseContext. Now, CryptAcquireContext is called once in PRNG::new, and CryptReleaseContext is called by a Cleaner, and nextBytes only calls CryptGenRandom. I haven't read about thread-safety in any MS document, the current Windows-PRNG service is marked ThreadSafe=true (in SunMSCAPI.java). If we cannot be really sure, we can change it to false. I've downloaded nearly 1000 Mach5 runs of this test, the enhancement is so good that I adjusted the test to be stricter. Before After ------ ----- Count 897 74 Min 0.38 0.008 Ave 0.97 0.011 Max 5.81 0.021 Please advise me if the following usage of Cleaner is correct because I really haven't observed the releaseContext method being called. + Cleaner.create().register(this, + () -> releaseContext(ctxt)); Thanks Max