首页 \ 问答 \ 在netty中的单例类中使用threadlocal(Using threadlocal in a singleton class in netty)

在netty中的单例类中使用threadlocal(Using threadlocal in a singleton class in netty)

我不确定是否存在任何线程安全问题,因为我使用单例util类并且有一个成员变量,起初,我使用threadlocal来尝试避免这些问题。 但是netty的Nio线程池太小了(大小只有4,因为cpu核心是2),所以我想知道当并发级别很高时存在一些线程安全问题,例如:

  1. nio-thread1正在处理requestA,并将threadLocal值设置为a

  2. 在完成处理之前,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:

  1. nio-thread1 is handling requestA, and set the threadLocal value to a

  2. 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
更新时间:2023-07-30 15:07

最满意答案

表示不可编辑的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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。