1. 程式人生 > >JUC學習之同步鎖和閉鎖

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();
            }
        }
    }
}