【并发编程】- Semaphore 、ReentrantLock多线程处理任务同步性

98 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第8天,点击查看活动详情

多进路-单处理-多出路

允许多个线程同时处理任务,但执行任务的顺序是同步的,也就是会阻塞的,所以称为单处理。

多线程执行各自任务,添加ReentrantLock实现同步具体代码如下:

@Slf4j
public class MoreThreadService {
    private Semaphore semaphore = new Semaphore(3);
    private ReentrantLock reentrantLock = new ReentrantLock();

    public void sayHello(){
        try {
            semaphore.acquire();
            log.info("线程名:{} 准备",Thread.currentThread().getName());
            reentrantLock.lock();
            log.info("开始打招呼 :{}",System.currentTimeMillis());
            for (int i = 0; i < 3 ; i++) {
                log.info("线程名:{},打印的值:{}",Thread.currentThread().getName(),i+1);
            }
            log.info("结束打招呼 :{}",System.currentTimeMillis());
            reentrantLock.unlock();
            semaphore.release();
            log.info("线程名:{} 结束",Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

运行结果如下:

16:07:52.675 [Thread-1] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-1 准备
16:07:52.704 [Thread-1] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 开始打招呼 :1649405272704
16:07:52.704 [Thread-1] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-1,打印的值:1
16:07:52.704 [Thread-1] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-1,打印的值:2
16:07:52.704 [Thread-1] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-1,打印的值:3
16:07:52.704 [Thread-1] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 结束打招呼 :1649405272704
16:07:52.704 [Thread-1] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-1 结束
16:07:52.675 [Thread-0] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-0 准备
16:07:52.705 [Thread-0] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 开始打招呼 :1649405272705
16:07:52.707 [Thread-0] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-0,打印的值:1
16:07:52.707 [Thread-0] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-0,打印的值:2
16:07:52.707 [Thread-0] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-0,打印的值:3
16:07:52.707 [Thread-0] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 结束打招呼 :1649405272707
16:07:52.707 [Thread-0] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-0 结束
16:07:52.675 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-2 准备
16:07:52.712 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 开始打招呼 :1649405272712
16:07:52.712 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-2,打印的值:1
16:07:52.712 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-2,打印的值:2
16:07:52.712 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-2,打印的值:3
16:07:52.713 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 结束打招呼 :1649405272713
16:07:52.713 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.service.MoreThreadService - 线程名:Thread-2 结束

可以看出加入了ReentrantLock对象,保证了同步性。