在netty中的单例类中使用threadlocal(Using threadlocal in a singleton class in netty)
我不确定是否存在任何线程安全问题,因为我使用单例util类并且有一个成员变量,起初,我使用threadlocal来尝试避免这些问题。 但是netty的Nio线程池太小了(大小只有4,因为cpu核心是2),所以我想知道当并发级别很高时存在一些线程安全问题,例如:
nio-thread1正在处理requestA,并将threadLocal值设置为a
在完成处理之前,requestE来了,nio-thread1来处理requestE,并将threadLocal值设置为e
那么,在这种情况下,requestA受到了影响吗? 如果是,如果我想将此值保留为成员变量(不将其放在方法中),我该如何避免呢?
谢谢你的任何建议!
这是我的代码:
/** * * @param <T> * source * @param <V> * result * @param <K> * key */ public interface BaseDecryption<S, R, K> { public static enum DecryType { AES128CBC, AES128XOR, XOR } public BaseDecryption<S, R, K> withDecryType(DecryType type); public DecryType getDecryType(); public R decrypt(S source); } public abstract class BytesDecryption implements BaseDecryption<byte[], byte[], byte[]> { private DecryType decrypTye; /** * Here is where I used the treadLocal * */ private ThreadLocal<byte[]> key = new ThreadLocal<byte[]>(); protected DecryType getDecrypTye() { return decrypTye; } protected byte[] getKey() { return this.key.get(); } public BaseDecryption<byte[], byte[], byte[]> withDecryKey(byte[] key) { this.key.set(key); return this; } @Override public BaseDecryption<byte[], byte[], byte[]> withDecryType( DecryType decryType) { this.decrypTye = decryType; return this; } } @Component("LEAD_AES128CBC") public class AES128CBC extends BytesDecryption { private AlgorithmParameters params; private static final String KEY_ALGORITHM = "AES"; public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding"; private static final Logger logger = LoggerFactory .getLogger(AES128CBC.class); public AES128CBC() throws NoSuchAlgorithmException, InvalidParameterSpecException { Security.addProvider(new BouncyCastleProvider()); this.withDecryType(DecryType.AES128CBC); initVi(); } @Override public byte[] decrypt(byte[] source) { byte[] key = getKey(); byte[] size16Key = new byte[16]; System.arraycopy(key, 0, size16Key, 0, 16); SecretKey secretKey = new SecretKeySpec(size16Key, KEY_ALGORITHM); try { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey, params); if (source.length % 16 != 0) { byte[] encryptionBytes = new byte[source.length - source.length % 16]; System.arraycopy(source, 0, encryptionBytes, 0, encryptionBytes.length); byte[] decryptionBytes = cipher.doFinal(encryptionBytes); byte[] finalBytes = new byte[decryptionBytes.length + source.length % 16]; System.arraycopy(decryptionBytes, 0, finalBytes, 0, 0); // only multiple of 16 bytes will be decrypted, so copy the // remained System.arraycopy(source, encryptionBytes.length, finalBytes, encryptionBytes.length, source.length % 16); return finalBytes; } return cipher.doFinal(source); } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; } @Override public DecryType getDecryType() { return DecryType.AES128CBC; } public void initVi() throws NoSuchAlgorithmException, InvalidParameterSpecException { byte[] iv = new byte[16]; Arrays.fill(iv, (byte) 0x00); params = AlgorithmParameters.getInstance(KEY_ALGORITHM); params.init(new IvParameterSpec(iv)); } public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } }
我用春天
BeanUtil.getBean(encryType) // encryType may equal to LEAD_AES128CBC
上课。
I am not sure if there exists any thread-safety issues cause I use a singleton util class and there is a member variable, at first, I used threadlocal to try to avoid these issues. But the Nio thread pool of netty is too much small(the size is only 4, cause the cpu core is 2), so I am wondering that there exists some thread-safety issues when the concurrent level is high, for example:
nio-thread1 is handling requestA, and set the threadLocal value to a
before it finished handling it, requestE comes and nio-thread1 comes to handle requestE, and set the threadLocal value to e
So, in this situation, was requestA influenced? If yes, how can I avoid it ,if I want to keep this value as a member variable(not put it to a method)
Thanks for any suggestion!
Here is my code:
/** * * @param <T> * source * @param <V> * result * @param <K> * key */ public interface BaseDecryption<S, R, K> { public static enum DecryType { AES128CBC, AES128XOR, XOR } public BaseDecryption<S, R, K> withDecryType(DecryType type); public DecryType getDecryType(); public R decrypt(S source); } public abstract class BytesDecryption implements BaseDecryption<byte[], byte[], byte[]> { private DecryType decrypTye; /** * Here is where I used the treadLocal * */ private ThreadLocal<byte[]> key = new ThreadLocal<byte[]>(); protected DecryType getDecrypTye() { return decrypTye; } protected byte[] getKey() { return this.key.get(); } public BaseDecryption<byte[], byte[], byte[]> withDecryKey(byte[] key) { this.key.set(key); return this; } @Override public BaseDecryption<byte[], byte[], byte[]> withDecryType( DecryType decryType) { this.decrypTye = decryType; return this; } } @Component("LEAD_AES128CBC") public class AES128CBC extends BytesDecryption { private AlgorithmParameters params; private static final String KEY_ALGORITHM = "AES"; public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding"; private static final Logger logger = LoggerFactory .getLogger(AES128CBC.class); public AES128CBC() throws NoSuchAlgorithmException, InvalidParameterSpecException { Security.addProvider(new BouncyCastleProvider()); this.withDecryType(DecryType.AES128CBC); initVi(); } @Override public byte[] decrypt(byte[] source) { byte[] key = getKey(); byte[] size16Key = new byte[16]; System.arraycopy(key, 0, size16Key, 0, 16); SecretKey secretKey = new SecretKeySpec(size16Key, KEY_ALGORITHM); try { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey, params); if (source.length % 16 != 0) { byte[] encryptionBytes = new byte[source.length - source.length % 16]; System.arraycopy(source, 0, encryptionBytes, 0, encryptionBytes.length); byte[] decryptionBytes = cipher.doFinal(encryptionBytes); byte[] finalBytes = new byte[decryptionBytes.length + source.length % 16]; System.arraycopy(decryptionBytes, 0, finalBytes, 0, 0); // only multiple of 16 bytes will be decrypted, so copy the // remained System.arraycopy(source, encryptionBytes.length, finalBytes, encryptionBytes.length, source.length % 16); return finalBytes; } return cipher.doFinal(source); } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; } @Override public DecryType getDecryType() { return DecryType.AES128CBC; } public void initVi() throws NoSuchAlgorithmException, InvalidParameterSpecException { byte[] iv = new byte[16]; Arrays.fill(iv, (byte) 0x00); params = AlgorithmParameters.getInstance(KEY_ALGORITHM); params.init(new IvParameterSpec(iv)); } public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } }
and I use spring
BeanUtil.getBean(encryType) // encryType may equal to LEAD_AES128CBC
to get the class.
原文:https://stackoverflow.com/questions/41235994
最满意答案
表示不可编辑的TextArea的更好方法是标签。 如果需要,这应该扩展到多行。 它也不太可能让用户混淆认为盒子是你可以编辑的东西,就像普通的textarea条目一样。
不确定图片正在尝试通信。 最初,在我看来,您正在以错误的顺序将视图和textarea添加到整个视图中,或者将其顶部/底部位置设置错误。
如果您正在尝试同时显示可滚动文本和包含其他内容的视图,我可能会创建一个Scrollable View并将我的文本放在具有适当高度设置的视图上,然后让另一个视图填充其余部分屏幕。
每当我在屏幕上创建大量文本或项目时,我的视图尚未滚动到底部。 如果我自己向下滚动并重新创建我的获胜/视图,那么我可能会看到这一点。 特别是如果我尝试重复使用屏幕上已显示的项目,只需重新加载数据。
Better way to represent a TextArea that isn't editable would be a label. This should expand to multiple lines if needed. It would also be less likely to confuse the user into thinking the box is something you can edit like a normal textarea entry.
Not really sure what the pictures are trying to communicate. Initially it looks like to me you are adding the view and the textarea to the overall view in the wrong order or have their top/bottom position set wrong.
If you are attempting to show scrollable text and a view with some other content on it at the same time, I would probably create a Scrollable View and put my text on that with the appropriate height set and then have the other view fill the rest of the screen.
Whenever I created a large amount of text or items on the screen, my view hasn't already been scrolled to the bottom. If I don't destroy and recreate my win/view after scrolling down on it myself, then I might see this. Especially if I attempt to reuse the items already displayed on the screen and just reload them with data.
相关问答
更多-
在UIView中定位文本(Positioning text in UIView)[2023-09-13]
1)对标签应用所需的autolayout并取3个NSLayoutConstraint IBOutlets属性,它们是1)标签宽度2)标签高度3)标签y Origin。 2)计算标签宽度让我们说标签宽度约束名称是:lblWidthConstraint **CGFrame screenFrame = [UIScreen mainScreen].bounds;** **if(screenframe.size.width>200){ self.lblWidthConstraint.con ... -
你放置 position: relative; 在背景div。 地点 Position: absolute; 在前景div和位置上, top, bottom, left, right 。 如果使用%设置前景的宽度,前景容器将相对于背景工作,这意味着它是响应的 You place position: relative; On the background div. Place Position: absolute; On the foreground div and position with top, bo ...
-
将图像和文本放在一个div下,并给出给图像的css,将其赋予新的div。 ie position:absolute到新创建的div。 喜欢: body{ background-color: rgb(47,47,47); font-family: "Lucida Grande", "Lucida Sans Unicode", Arial, sans-serif; letter-spacing: -1px; color: rgb(230,230,230); } u ...
-
通常我使用CSS position来做那种事情。 设置父position:relative ,将position:absolute添加到::after元素,并给它一个top:100% http://jsfiddle.net/s22Af/ div#myImg:after{ content: 'TEXT UNDER IMAGE'; margin:0 auto; vertical-align:text-bottom; font-size: 14px; position:a ...
-
读取UIWebView文本内容(Reading UIWebView TEXT content)[2023-09-07]
获取.textContent或.innerText ,而不是.innerHTML 。 [web stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText;"]; (顺便说一句,还有一个私人财产webView.text 。) Get the .textContent or .innerText of the root node, instead of .innerHTML. [web stringByEvaluat ... -
CSS定位和文本(CSS Positioning & text)[2022-09-11]
将.register按钮的宽度设置为300px。 这是一个使用你的代码的jsFiddle示例( http://jsfiddle.net/2WCje/ ) CSS: .register-button { padding:7px 375px; background: #3b7aae; border-radius: 5px; border: none; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .25), inset 0 1px 0 ... -
问题出在refreshNotes()函数中。 删除var oldText以及将其作为值传递到textarea的行解决了这个问题。 The problem was in the refreshNotes() function. Removing the var oldText and the line where it was passed as value to the textarea solved the issue.
-
我不知道它是否有效,但尝试删除 机器人:layout_gravity = “中心” 来自相对布局的线 I don't know will it work or not but try removing android:layout_gravity="center" line from Relative Layout
-
表示不可编辑的TextArea的更好方法是标签。 如果需要,这应该扩展到多行。 它也不太可能让用户混淆认为盒子是你可以编辑的东西,就像普通的textarea条目一样。 不确定图片正在尝试通信。 最初,在我看来,您正在以错误的顺序将视图和textarea添加到整个视图中,或者将其顶部/底部位置设置错误。 如果您正在尝试同时显示可滚动文本和包含其他内容的视图,我可能会创建一个Scrollable View并将我的文本放在具有适当高度设置的视图上,然后让另一个视图填充其余部分屏幕。 每当我在屏幕上创建大量文本或项 ...
-
在页面源代码中可以看到,开始和结束都有一定的空间。 也许get_film_info()会无意中注入它们。 There's definitely a space in the beginning and one at the end, as can be seen in the page source. Perhaps get_film_info() is inadvertently injecting them.