首页

分享cedarsoft源码中通过LogingReentrantLock实现java.util.concurrent.locks.ReadWriteLock读写锁自定义源码

标签:concurrent,并发包,读写锁lock,队列锁,ReadWriteLock,LogingReentrantLock,cedarsoft,同步控制     发布时间:2018-01-14   

一、前言

关于cedarsoft开源concurrent-5.0.0的并发包定义com.cedarsoft.concurrent.LogingReentrantLock读写锁,继承java.util.concurrent.locks.ReadWriteLock基本锁,同步依赖定义com.cedarsoft.concurrent.LogingLock、com.cedarsoft.concurrent.DelegatingLock锁的基本职能,具体参考下面源码说明。

分享cedarsoft源码中通过LogingReentrantLock实现java.util.concurrent.locks.ReadWriteLock读写锁自定义源码

二、源码说明

package com.cedarsoft.concurrent;@b@@b@import java.util.ArrayList;@b@import java.util.Collections;@b@import java.util.List;@b@import java.util.concurrent.TimeUnit;@b@import java.util.concurrent.locks.Condition;@b@import java.util.concurrent.locks.Lock;@b@import java.util.concurrent.locks.ReadWriteLock;@b@import java.util.concurrent.locks.ReentrantReadWriteLock;@b@import javax.annotation.Nonnull;@b@@b@public class LogingReentrantLock@b@  implements ReadWriteLock@b@{@b@@b@  @Nonnull@b@  private final ReadWriteLock delegate;@b@@b@  @Nonnull@b@  private final List<Thread> readLockingThreads;@b@  private final List<Thread> writeLockingThreads;@b@@b@  @Nonnull@b@  private final LogingLock readLock;@b@@b@  @Nonnull@b@  private final LogingLock writeLock;@b@@b@  public LogingReentrantLock()@b@  {@b@    this.delegate = new ReentrantReadWriteLock();@b@@b@    this.readLockingThreads = new ArrayList();@b@@b@    this.writeLockingThreads = new ArrayList();@b@@b@    this.readLock = new LogingLock(this.delegate.readLock(), this.readLockingThreads, null);@b@@b@    this.writeLock = new LogingLock(this, this.delegate.writeLock(), this.writeLockingThreads)@b@    {@b@      public void lock()@b@      {@b@        if (!(LogingReentrantLock.access$100(this.this$0).isEmpty()))@b@          throw new InvalidLockStateException(LogingReentrantLock.access$100(this.this$0));@b@@b@        super.lock();@b@      }@b@    };@b@  }@b@@b@  public Lock readLock()@b@  {@b@    return this.readLock;@b@  }@b@@b@  public Lock writeLock()@b@  {@b@    return this.writeLock;@b@  }@b@@b@  private static class LogingLock extends LogingReentrantLock.DelegatingLock@b@  {@b@@b@    @Nonnull@b@    private final List<Thread> lockingThreads;@b@@b@    private LogingLock(@Nonnull Lock delegate, @Nonnull List<Thread> lockingThreads)@b@    {@b@      super(delegate);@b@      this.lockingThreads = lockingThreads;@b@    }@b@@b@    public void lock()@b@    {@b@      this.lockingThreads.add(0, Thread.currentThread());@b@      super.lock();@b@    }@b@@b@    public void unlock()@b@    {@b@      this.lockingThreads.remove(Thread.currentThread());@b@      super.unlock();@b@    }@b@@b@    @Nonnull@b@    public List<? extends Thread> getLockingThreads() {@b@      return Collections.unmodifiableList(this.lockingThreads);@b@    }@b@  }@b@@b@  private static class DelegatingLock@b@    implements Lock@b@  {@b@    private final Lock delegate;@b@@b@    protected DelegatingLock(@Nonnull Lock delegate)@b@    {@b@      this.delegate = delegate;@b@    }@b@@b@    public void lock()@b@    {@b@      this.delegate.lock();@b@    }@b@@b@    public void lockInterruptibly() throws InterruptedException@b@    {@b@      this.delegate.lockInterruptibly();@b@    }@b@@b@    public boolean tryLock()@b@    {@b@      return this.delegate.tryLock();@b@    }@b@@b@    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException@b@    {@b@      return this.delegate.tryLock(time, unit);@b@    }@b@@b@    public void unlock()@b@    {@b@      this.delegate.unlock();@b@    }@b@@b@    public Condition newCondition()@b@    {@b@      return this.delegate.newCondition();@b@    }@b@  }@b@}

��

<<热门下载>>