0
点赞
收藏
分享

微信扫一扫

concurrent包的同步控制工具

书呆鱼 2021-09-28 阅读 88
码哥

ReentrantLock

  • 可以响应线程中断:
    ReentrantLock lock = new ReentrantLock();
    try {
        lock.lockInterruptibly();
    } catch (InterruptedException e) {

    } finally {
        if (lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
  • 可以限时,这样可以避免死锁
       ReentrantLock lock = new ReentrantLock();
        try {
            if (lock.tryLock(5, TimeUnit.SECONDS)) {

            } else {
            
            }
        } catch (InterruptedException e) {

        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
  • ReentrantLock可以实现公平锁:

ReentrantLock lock = new ReentrantLock(true);

  • 通过Condition类来挂起唤醒线程,condition和Object的wait和notify很像
  Condition condition = lock.newCondition();  
  condition.await();  
  condition.await(等待时间);  
  condition.awaitUninterruptibly();  
  condition.signal();  
  condition.signalAll();

Semaphore信号量

  • acquire()
  • acquireUninterruptibly()
  • tryacquire()、tryacquire(long,TimeUnit)
  • release()
    Semaphore semaphore = new Semaphore(5);
    try {
        semaphore.acquire();
    } catch (InterruptedException e) {
    
    } finally {
        semaphore.release();
    }

ReadWriteLock读写锁

  • readLock()获得ReentrantReadWriteLock.ReadLock对象
  • writeLock()获得ReentrantReadWriteLock.WriteLock对象
    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    Lock readLock = readWriteLock.readLock();
    Lock writeLock = readWriteLock.writeLock();
    readLock.lock();//读锁和写锁的使用和ReentrantLock一样: 
    writeLock.lock();

CountDownLatch倒数计时器

public class CountDownLatchDemo implements Runnable{
    private static CountDownLatch latch = new CountDownLatch(5);
    
    @Override
    public void run() {
        //一些检查工作,做完之后倒数计数器减一
        latch.countDown();
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            exec.submit(new CountDownLatchDemo());
        }
        latch.await();//五个检查工作都完成才会继续执行
    }
}

CyclicBarrier循环栅栏

  • 构造方法:CyclicBarrier(int parties, Runnable barrierAction)
  • await()
public class BarrierDemo implements Runnable {
    int flag = 0;
    @Override
    public void run() {
        if (flag == 0) {
            //TODO
        } else {
            //TODO
        }
        flag++;
    }
    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier barrier = new CyclicBarrier(10, new BarrierDemo());
        for (int i = 0; i < 10; i++) {
            new Thread(new Prepare(barrier)).start();
        }
        Thread.sleep(5000);
    }
}
class Prepare implements Runnable {
    CyclicBarrier barrier;
    public Prepare(CyclicBarrier barrier) {
        this.barrier = barrier;
    }
    @Override
public void run() {
        try {
            barrier.await();//十个线程全部await之后,继续向下执行并触发barrier的执行线程,并清零计数器
            //TODO
            barrier.await();//复用计数器开始下一轮
            //TODO
        } catch (InterruptedException ie) {

        } catch (BrokenBarrierException be) {//有线程被中断后,其他的线程抛出该异常

        }
    }}

LockSupport

  • LockSupport.pack()
  • LockSupport.unpack(Thread)

举报

相关推荐

0 条评论