Research thread lock of RLock (a)

Deadlock: refers to the phenomenon of two or more processes or threads in the implementation process, a result of competition for resources caused by waiting for each other, in the absence of external force, they will not be able to promote it. At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock
 

 RLock lock = redissonClient.getLock(lockKey);

        lock.lock(2, TimeUnit.SECONDS);

        try {
            RDeque<String> deque = redissonClient.getDeque(userKey);

            // 如果队列里没有此token,且用户没有被踢出;放入队列
            if (!deque.contains(token) && currentSession.isKickout() == false) {
                deque.push(token);
            }

            // 如果队列里的sessionId数超出最大会话数,开始踢人
            while (deque.size() > maxSession) {
                String kickoutSessionId;
                if (kickoutAfter) { // 如果踢出后者
                    kickoutSessionId = deque.removeFirst();
                } else { // 否则踢出前者
                    kickoutSessionId = deque.removeLast();
                }

                try {
                    RBucket<UserBO> bucket = redissonClient.getBucket(kickoutSessionId);
                    UserBO kickoutSession = bucket.get();

                    if (kickoutSession != null) {
                        // 设置会话的kickout属性表示踢出了
                        kickoutSession.setKickout(true);
                        bucket.set(kickoutSession);
                    }

                } catch (Exception e) {
                }

            }

            // 如果被踢出了,直接退出,重定向到踢出后的地址
            if (currentSession.isKickout()) {
                // 会话被踢出了
                try {
                    // 注销
                    userService.logout(token);
                    sendJsonResponse(response, 4001, "您的账号已在其他设备登录");

                } catch (Exception e) {
                }

                return false;

            }

        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
                LOGGER.info(Thread.currentThread().getName() + " unlock");

            } else {
                LOGGER.info(Thread.currentThread().getName() + " already automatically release lock");
            }
        }

So how to solve the deadlock phenomenon?

Solution, recursive lock: In order to support Python, in the same thread multiple requests for the same resource, python provides reentrant lock RLock. This internal RLock maintains a Lock and a counter variable, counter records the number of times acquire, so that resources can be many times require. Acquire a thread until all have been release, other threads to get resources. The above example if instead of using RLock Lock, deadlock will not occur.

threading.Lock and threading.RLock difference: RLock be allowed to acquire multiple times in the same thread. The Lock does not allow this. Note: If you use RLock, then acquire and release must be paired, that is called n times acquire, you must call the n-th release can really release the occupied petty.

basic concept

distributed

A plurality of modules in the system deployed on different servers, can be called a distributed system, such as Tomcat and databases are deployed on a different server, or both Tomcat same function are deployed on a different server

High Availability

When some nodes in the system fails, the other node can take over continue to provide services, it can be considered to have a high-availability system

Clusters

A software specific areas of deployment and as a whole provides a class of service on multiple servers, called the whole cluster. Zookeeper as the Master and Slave are deployed on multiple servers together to form a whole provides centralized configuration services.

In a typical cluster, clients are often able to connect any node access to services, and when dropped cluster node, the other nodes are often able to automatically take over continue to provide services, this time with a high-availability cluster description

Load Balancing

When the request is sent to the system, in some way by the request is distributed evenly to the plurality of nodes, each node of the system can be uniformly processing request load can be considered a load balancing system

Forward Proxy and Reverse Proxy

When internal systems to access external networks, unified by a proxy server forwards the request out, it seems that the proxy server initiated in external network access, then the proxy server implementation is forward proxy; when the external request enters the system, the proxy server the request is forwarded to a server system, to external requests, the proxy server interacts only, then the proxy server is implemented reverse proxy.

Briefly, forward proxy is a proxy server instead of internal system procedures to access the external network, and the reverse proxy forwarding the request to access the system via the external proxy server to the internal server process.

Guess you like

Origin blog.csdn.net/qq_37996327/article/details/92804522