JUC学习之同步锁和闭锁

一、同步锁(Lock):同步锁是一个显示锁,需要通过 lock() 上锁,通过 unlock() 释放锁

public class Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Ticket ticket = new Ticket();
        new Thread(ticket, "1号窗口").start();
        new Thread(ticket, "2号窗口").start();
        new Thread(ticket, "3号窗口").start();
    }
}

class Ticket implements Runnable {

    private int tickets = 100;
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        // 加锁
        lock.lock();
        try {
            while (tickets > 0) {
                Thread.sleep(100);
                System.out.println(Thread.currentThread().getName() + " 完成售票,余票为:" + --tickets);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }
}

二、闭锁(CountDownLatch):在要完成某些运算时,只有其它线程的运算全部运行完毕,当前运算才继续下去。

例如:计算线程的执行时间

public class Test {

    public static void main(String[] args) {
        // 闭锁计数器和其它线程数一样
        final CountDownLatch latch = new CountDownLatch(5);
        LatchDemo ld = new LatchDemo(latch);

        long start = System.currentTimeMillis();
        for (int i = 0; i < 5; i++) {
            // 其它运算线程开始执行
            new Thread(ld).start();
        }
        try {
            // 主线程等待其它线程全部执行完,才继续下去,算出线程执行时间
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start));
    }
}

// 其它运算线程
class LatchDemo implements Runnable {
    private CountDownLatch latch;

    LatchDemo(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        synchronized (this) {
            try {
                for (int i = 0; i < 50000; i++) {
                    if (i % 2 == 0) {
                        System.out.println(i);
                    }
                }
            } finally {
                // 闭锁每次减一
                latch.countDown();
            }
        }
    }
}

你可能感兴趣的:(java)