多线程

作者: jxcyly1985 | 来源:发表于2022-01-11 18:00 被阅读0次

    java中线程池的生命周期与线程中断

    Work实现了独占锁,是独占的同步锁对象

      protected boolean tryAcquire(int unused) {
                if (compareAndSetState(0, 1)) {
                    setExclusiveOwnerThread(Thread.currentThread());
                    return true;
                }
                return false;
            }
    
    

    Worker本身是AbstractQueuedSynchronizer条件队列对象,因为在shutdown和runWorker中可以利用Worker对象来做同步

    性能分析之– JAVA Thread Dump 分析

    深入理解Java并发之synchronized实现原理

    https://www.cnblogs.com/tera/p/14020276.html

    让面试官心服口服:Thread.sleep、synchronized、LockSupport.park的线程阻塞有何区别?

    synchronized底层实现monitor详解

    Java的synchronized原理与Monitor对象

    java并发系列-monitor机制实现

    公平锁和非公平锁

    static final class FairSync extends Sync {
            private static final long serialVersionUID = -3000897897090466540L;
    
            final void lock() {
                acquire(1);
            }
    
            /**
             * Fair version of tryAcquire.  Don't grant access unless
             * recursive call or no waiters or is first.
             */
            // Android-removed: @ReservedStackAccess from OpenJDK 9, not available on Android.
            // @ReservedStackAccess
            protected final boolean tryAcquire(int acquires) {
                final Thread current = Thread.currentThread();
                int c = getState();
                if (c == 0) {
                    if (!hasQueuedPredecessors() &&
                        compareAndSetState(0, acquires)) {
                        setExclusiveOwnerThread(current);
                        return true;
                    }
                }
                else if (current == getExclusiveOwnerThread()) {
                    int nextc = c + acquires;
                    if (nextc < 0)
                        throw new Error("Maximum lock count exceeded");
                    setState(nextc);
                    return true;
                }
                return false;
            }
        }
    
    

    公平锁增加了判断当前线程节点在条件队列中是否存在其他线程节点,如果存在则重新阻塞,增加休眠唤醒的次数,从而性能比非公平锁差

     /**
         * Wakes up node's successor, if one exists.
         *
         * @param node the node
         */
        private void unparkSuccessor(Node node) {
            /*
             * If status is negative (i.e., possibly needing signal) try
             * to clear in anticipation of signalling.  It is OK if this
             * fails or if status is changed by waiting thread.
             */
            int ws = node.waitStatus;
            if (ws < 0)
                node.compareAndSetWaitStatus(ws, 0);
    
            /*
             * Thread to unpark is held in successor, which is normally
             * just the next node.  But if cancelled or apparently null,
             * traverse backwards from tail to find the actual
             * non-cancelled successor.
             */
            Node s = node.next;
            if (s == null || s.waitStatus > 0) {
                s = null;
                for (Node p = tail; p != node && p != null; p = p.prev)
                    if (p.waitStatus <= 0)
                        s = p;
            }
            if (s != null)
                LockSupport.unpark(s.thread);
        }
    

    非公平模式唤醒的线程则是当前unlock线程节点的下一个可用线程节点

    相关文章

      网友评论

        本文标题:多线程

        本文链接:https://www.haomeiwen.com/subject/vlkjgktx.html