ConcurrentHashMapのputIfAbsentメソッドの使用

1000 ワード

    /**
    * @Description           
    * @Date   2019/3/21 16:46
    * @Param  [user, currentSessionId]
    * @Return      boolean
    * @Exception
    *
    */
    public static boolean OneUserOnlineCheck(User user, String currentSessionId) {

        String userId = user.getUSER_ID();
        /**  key    value    put,   null。  key    value   ,
                , value   null,         。*/
        String mapSession = oneUserCheckMap.putIfAbsent(userId,currentSessionId);

        if (mapSession == null){
            return true;
        }
        return mapSession.equals(currentSessionId);
//        if (!oneUserCheckMap.containsKey(userId)) {
//            oneUserCheckMap.put(userId, currentSessionId);
//            return true;
//        } else {
//            String mapSessionId = oneUserCheckMap.get(userId);
//            if (mapSessionId.equals(currentSessionId)) {
//                return true;
//            }
//            return false;
//        }
    }