import java.util.concurrent.locks.ReentrantLock;
public class Main{
public static void main(String[] args) {
//锁池
ReentrantLock[] locks= new ReentrantLock[1024];
//需要根据字符串a上锁
String a="lockStr";
//获取锁的索引位置
int index=a.hashCode()&locks.length;
//第一种锁方式
locks[index].lock();
//第二种锁方式
synchronized(locks[index]){
}
}
}
版权声明:本文为qq_32182637原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。