本文介绍了新新旗新新新旗新新新新旗新新旗新新旗新新旗新新旗旗新新旗新新新旗新旗新新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需一个 javax.crypto.Cipher 的实例(例如 Cipher.getInstance(RSA))从多个线程使用,或者我需要在 ThreadLocal (在我的情况下)中保留多个?

Quite simply, can one instance of javax.crypto.Cipher (e.g. Cipher.getInstance("RSA")) be used from multiple threads, or do I need to stick multiple of them in a ThreadLocal (in my case)?

推荐答案

不,不是。实例是有状态的。所以你需要将它存储在threadlocal中,或者在每个加密/解密调用中获取一个新的实例,或者将它包装在一个 synchronized(cipher)块中。

No, it isn't. The instance is stateful. So you need to store it threadlocal, or to obtain a new instance on every encrypt/decrypt call, or to wrap it in a synchronized(cipher) block.

线程安全通常是在javadocs中提到。 ,所以你不应该认为它是线程安全的。

Threadsafety is usually explicitly mentioned in javadocs. This is not the case for Cipher, so you should not assume it to be threadsafe.

这篇关于新新旗新新新旗新新新新旗新新旗新新旗新新旗新新旗旗新新旗新新新旗新旗新新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:10