相关疑难解决方法(0)

这段代码是线程安全的吗?

我想处理客户端请求流.每个请求都有其特殊类型.首先,我需要为该类型初始化一些数据,然后我可以开始处理请求.当客户端类型第一次出现时,我只是初始化相应的数据.在此之后,使用该数据处理该类型的所有以下请求.

我需要以线程安全的方式执行此操作.

这是我写的代码.它是线程安全的吗?

public class Test {

    private static Map<Integer, Object> clientTypesInitiated = new ConcurrentHashMap<Integer, Object>();

    /* to process client request we need to 
    create corresponding client type data.
    on the first signal we create that data, 
    on the second - we process the request*/

    void onClientRequestReceived(int clientTypeIndex) {
        if (clientTypesInitiated.put(clientTypeIndex, "") == null) {
            //new client type index arrived, this type was never processed
            //process data for that client type and put it into the map of types
            Object …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading concurrenthashmap

6
推荐指数
1
解决办法
335
查看次数