You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReentrantLock未达预期效果:线程未互斥执行问题排查

问题根源分析

哦,这个问题我太熟了!你的ReentrantLock完全没起到互斥作用,核心原因很简单——每个MyThread2实例都单独创建了一个属于自己的ReentrantLock对象。打个比方,就像两个各拿自己家门钥匙的人,各自开自己家的门,当然不会互相等对方进门,自然能同时执行。

解决办法:让所有线程共享同一个锁实例

要实现线程间的互斥,必须让所有需要同步的线程使用同一个ReentrantLock对象。有两种常见的实现方式:

方式1:把锁声明为静态变量

静态变量属于类而非实例,所有线程实例都会共享这个锁:

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

class MyThread2 extends Thread{
    String name;
    // 静态锁:所有线程共享同一个实例
    static ReentrantLock reentrantLock = new ReentrantLock();
    
    MyThread2(String name){
        this.name = name;
    }
    
    public void run(){
        do {
            try {
                if (reentrantLock.tryLock(20, TimeUnit.MILLISECONDS)){
                    try {
                        System.out.println("executing : "+ name);
                        Thread.sleep(500);
                    } finally {
                        reentrantLock.unlock(); // 放在finally确保锁一定会释放
                    }
                    break;
                }else {
                    System.out.println("waiting "+ name);
                    Thread.sleep(50); // 加个短休眠避免循环空转占CPU
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        } while(true);
    }
}

方式2:外部创建锁并传入线程

这种方式更灵活,适合需要控制锁作用范围的场景:

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

// 外部创建共享锁
public class Main {
    public static void main(String[] args) {
        ReentrantLock sharedLock = new ReentrantLock();
        MyThread2 thread1 = new MyThread2("Thread-1", sharedLock);
        MyThread2 thread2 = new MyThread2("Thread-2", sharedLock);
        
        thread1.start();
        thread2.start();
    }
}

class MyThread2 extends Thread{
    String name;
    ReentrantLock reentrantLock;
    
    // 构造方法接收外部传入的共享锁
    MyThread2(String name, ReentrantLock reentrantLock){
        this.name = name;
        this.reentrantLock = reentrantLock;
    }
    
    public void run(){
        do {
            try {
                if (reentrantLock.tryLock(20, TimeUnit.MILLISECONDS)){
                    try {
                        System.out.println("executing : "+ name);
                        Thread.sleep(500);
                    } finally {
                        reentrantLock.unlock();
                    }
                    break;
                }else {
                    System.out.println("waiting "+ name);
                    Thread.sleep(50);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        } while(true);
    }
}
额外小提示

使用tryLock时,一定要把unlock()放在finally块里!这样就算执行过程中抛出异常,锁也能被正常释放,不会导致其他线程永远拿不到锁的死锁问题。

内容的提问来源于stack exchange,提问作者emon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:21:29