并发编程(二) Lock用法

一、锁定类

package com.zsx.demo.lock;

import lombok.extern.log4j.Log4j2;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Log4j2
public class LockClass {

    private static final Lock lock = new ReentrantLock();

    public void print() {
        lock.lock();
        try {
            log.info("Current thread {} enters the print method, enter current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
            TimeUnit.SECONDS.sleep(2);
            log.info("Current thread {} exits the print method, exit current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                LockClass lockClass = new LockClass();
                lockClass.print();
                lockClass.print();
            }, String.format("t%d", i)).start();
        }
    }

}

1.运行结果

14:06:19.471 [t0] INFO com.zsx.demo.lock.LockClass - Current thread t0 enters the print method, enter current time : 2019-12-05T14:06:19.466575
14:06:21.476 [t0] INFO com.zsx.demo.lock.LockClass - Current thread t0 exits the print method, exit current time : 2019-12-05T14:06:21.476431400
14:06:21.477 [t0] INFO com.zsx.demo.lock.LockClass - Current thread t0 enters the print method, enter current time : 2019-12-05T14:06:21.476431400
14:06:23.478 [t0] INFO com.zsx.demo.lock.LockClass - Current thread t0 exits the print method, exit current time : 2019-12-05T14:06:23.478086200
14:06:23.478 [t2] INFO com.zsx.demo.lock.LockClass - Current thread t2 enters the print method, enter current time : 2019-12-05T14:06:23.478889
14:06:25.480 [t2] INFO com.zsx.demo.lock.LockClass - Current thread t2 exits the print method, exit current time : 2019-12-05T14:06:25.480727300
14:06:25.481 [t2] INFO com.zsx.demo.lock.LockClass - Current thread t2 enters the print method, enter current time : 2019-12-05T14:06:25.481491600
14:06:27.482 [t2] INFO com.zsx.demo.lock.LockClass - Current thread t2 exits the print method, exit current time : 2019-12-05T14:06:27.482367100
14:06:27.483 [t1] INFO com.zsx.demo.lock.LockClass - Current thread t1 enters the print method, enter current time : 2019-12-05T14:06:27.482367100
14:06:29.484 [t1] INFO com.zsx.demo.lock.LockClass - Current thread t1 exits the print method, exit current time : 2019-12-05T14:06:29.484020800
14:06:29.484 [t1] INFO com.zsx.demo.lock.LockClass - Current thread t1 enters the print method, enter current time : 2019-12-05T14:06:29.484020800
14:06:31.485 [t1] INFO com.zsx.demo.lock.LockClass - Current thread t1 exits the print method, exit current time : 2019-12-05T14:06:31.485670600
14:06:31.486 [t3] INFO com.zsx.demo.lock.LockClass - Current thread t3 enters the print method, enter current time : 2019-12-05T14:06:31.486439
14:06:33.487 [t3] INFO com.zsx.demo.lock.LockClass - Current thread t3 exits the print method, exit current time : 2019-12-05T14:06:33.487110600
14:06:33.487 [t3] INFO com.zsx.demo.lock.LockClass - Current thread t3 enters the print method, enter current time : 2019-12-05T14:06:33.487110600
14:06:35.487 [t3] INFO com.zsx.demo.lock.LockClass - Current thread t3 exits the print method, exit current time : 2019-12-05T14:06:35.487958200
14:06:35.488 [t4] INFO com.zsx.demo.lock.LockClass - Current thread t4 enters the print method, enter current time : 2019-12-05T14:06:35.488767200
14:06:37.489 [t4] INFO com.zsx.demo.lock.LockClass - Current thread t4 exits the print method, exit current time : 2019-12-05T14:06:37.489627300
14:06:37.489 [t4] INFO com.zsx.demo.lock.LockClass - Current thread t4 enters the print method, enter current time : 2019-12-05T14:06:37.489627300
14:06:39.491 [t4] INFO com.zsx.demo.lock.LockClass - Current thread t4 exits the print method, exit current time : 2019-12-05T14:06:39.491256800

二、锁定对象

package com.zsx.demo.lock;

import lombok.extern.log4j.Log4j2;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Log4j2
public class LockObject {

    private final Lock lock = new ReentrantLock();

    public void print() {
        lock.lock();
        try {
            log.info("Current thread {} enters the print method, enter current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
            TimeUnit.SECONDS.sleep(2);
            log.info("Current thread {} exits the print method, exit current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                LockObject lockObject = new LockObject();
                lockObject.print();
                lockObject.print();
            }, String.format("t%d", i)).start();
        }
    }

}

2.运行结果

14:12:00.627 [t2] INFO com.zsx.demo.lock.LockObject - Current thread t2 enters the print method, enter current time : 2019-12-05T14:12:00.621256700
14:12:00.627 [t3] INFO com.zsx.demo.lock.LockObject - Current thread t3 enters the print method, enter current time : 2019-12-05T14:12:00.621256700
14:12:00.627 [t1] INFO com.zsx.demo.lock.LockObject - Current thread t1 enters the print method, enter current time : 2019-12-05T14:12:00.621256700
14:12:00.627 [t0] INFO com.zsx.demo.lock.LockObject - Current thread t0 enters the print method, enter current time : 2019-12-05T14:12:00.621256700
14:12:00.626 [t4] INFO com.zsx.demo.lock.LockObject - Current thread t4 enters the print method, enter current time : 2019-12-05T14:12:00.621256700
14:12:02.633 [t1] INFO com.zsx.demo.lock.LockObject - Current thread t1 exits the print method, exit current time : 2019-12-05T14:12:02.633123500
14:12:02.633 [t3] INFO com.zsx.demo.lock.LockObject - Current thread t3 exits the print method, exit current time : 2019-12-05T14:12:02.633123500
14:12:02.633 [t0] INFO com.zsx.demo.lock.LockObject - Current thread t0 exits the print method, exit current time : 2019-12-05T14:12:02.633123500
14:12:02.633 [t2] INFO com.zsx.demo.lock.LockObject - Current thread t2 exits the print method, exit current time : 2019-12-05T14:12:02.633123500
14:12:02.633 [t1] INFO com.zsx.demo.lock.LockObject - Current thread t1 enters the print method, enter current time : 2019-12-05T14:12:02.633897200
14:12:02.633 [t3] INFO com.zsx.demo.lock.LockObject - Current thread t3 enters the print method, enter current time : 2019-12-05T14:12:02.633897200
14:12:02.633 [t0] INFO com.zsx.demo.lock.LockObject - Current thread t0 enters the print method, enter current time : 2019-12-05T14:12:02.633897200
14:12:02.633 [t2] INFO com.zsx.demo.lock.LockObject - Current thread t2 enters the print method, enter current time : 2019-12-05T14:12:02.633897200
14:12:02.633 [t4] INFO com.zsx.demo.lock.LockObject - Current thread t4 exits the print method, exit current time : 2019-12-05T14:12:02.633123500
14:12:02.634 [t4] INFO com.zsx.demo.lock.LockObject - Current thread t4 enters the print method, enter current time : 2019-12-05T14:12:02.634905600
14:12:04.634 [t0] INFO com.zsx.demo.lock.LockObject - Current thread t0 exits the print method, exit current time : 2019-12-05T14:12:04.634749600
14:12:04.634 [t1] INFO com.zsx.demo.lock.LockObject - Current thread t1 exits the print method, exit current time : 2019-12-05T14:12:04.634749600
14:12:04.634 [t3] INFO com.zsx.demo.lock.LockObject - Current thread t3 exits the print method, exit current time : 2019-12-05T14:12:04.634749600
14:12:04.634 [t2] INFO com.zsx.demo.lock.LockObject - Current thread t2 exits the print method, exit current time : 2019-12-05T14:12:04.634749600
14:12:04.635 [t4] INFO com.zsx.demo.lock.LockObject - Current thread t4 exits the print method, exit current time : 2019-12-05T14:12:04.635527600
发布了129 篇原创文章 · 获赞 14 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/zsx18273117003/article/details/103403589
今日推荐