Java 单向同步:如何阻止一个特定的方法?
各位用户好 我目前正在编写一个类,其中的实例将用作JavaBeanJava 单向同步:如何阻止一个特定的方法?,java,synchronization,locking,thread-safety,Java,Synchronization,Locking,Thread Safety,各位用户好 我目前正在编写一个类,其中的实例将用作JavaBeanPropertyDescriptors的缓存。您可以调用一个方法getPropertyDescriptor(Class clazz,String propertyName),该方法将返回相应的PropertyDescriptor。如果以前没有检索到该类,则会获取该类的BeanInfo实例并找到正确的描述符。然后为类名对存储此结果,以便下次可以立即返回,而无需查找或使用BeanInfo 第一个问题是同一类的多个调用会重叠。这可以通过
PropertyDescriptors
的缓存。您可以调用一个方法getPropertyDescriptor(Class clazz,String propertyName)
,该方法将返回相应的PropertyDescriptor
。如果以前没有检索到该类,则会获取该类的BeanInfo
实例并找到正确的描述符。然后为类名对存储此结果,以便下次可以立即返回,而无需查找或使用BeanInfo
第一个问题是同一类的多个调用会重叠。这可以通过同步clazz参数来简单地解决。因此,对同一类的多个调用是同步的,但对不同类的调用可以不受阻碍地继续。这似乎是线程安全性和活动性之间的一个不错的折衷
现在,有可能在某个时候,某些已经内省的类可能需要卸载。我不能简单地保留对它们的引用,因为这可能会导致类加载器泄漏。另外,JavaBeans API的Introspector
类提到类加载器的销毁应该与Introspector的刷新相结合:
因此,我添加了一个方法flushDirectory(ClassLoader cl)
,该方法将从缓存中删除任何类,并将其从内窥镜检查器中刷新(使用Introspector.flushFromCaches(class clz)
),前提是它是使用该类加载器加载的
现在我有一个关于同步的新问题。当此刷新正在进行时,不应将新映射添加到缓存中,而如果访问仍在进行,则不应启动刷新。换言之,基本问题是:
如何确保一段代码可以由多个线程运行,而另一段代码只能由一个线程运行,并禁止其他代码运行?这是一种单向同步
首先,我尝试结合使用java.util.concurrent.Lock
和AtomicInteger
来记录正在进行的调用数量,但注意到锁只能获得,如果当前正在使用而没有锁定,则无法检查。现在我在原子整数上的对象上使用简单同步。这是我的班级精简版:
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class DescriptorDirectory {
private final ClassPropertyDirectory classPropertyDirectory = new ClassPropertyDirectory();
private final Object flushingLock = new Object();
private final AtomicInteger accessors = new AtomicInteger(0);
public DescriptorDirectory() {}
public PropertyDescriptor getPropertyDescriptor(final Class<?> clazz, final String propertyName) throws Exception {
//First incrementing the accessor count.
synchronized(flushingLock) {
accessors.incrementAndGet();
}
PropertyDescriptor result;
//Synchronizing on the directory Class root
//This is preferrable to a full method synchronization since two lookups for
//different classes can never be on the same directory path and won't collide
synchronized(clazz) {
result = classPropertyDirectory.getPropertyDescriptor(clazz, propertyName);
if(result == null) {
//PropertyDescriptor wasn't loaded yet
//First we need bean information regarding the parent class
final BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch(final IntrospectionException e) {
accessors.decrementAndGet();
throw e;
//TODO: throw specific
}
//Now we must find the PropertyDescriptor of our target property
final PropertyDescriptor[] propList = beanInfo.getPropertyDescriptors();
for (int i = 0; (i < propList.length) && (result == null); i++) {
final PropertyDescriptor propDesc = propList[i];
if(propDesc.getName().equals(propertyName))
result = propDesc;
}
//If no descriptor was found, something's wrong with the name or access
if(result == null) {
accessors.decrementAndGet();
//TODO: throw specific
throw new Exception("No property with name \"" + propertyName + "\" could be found in class " + clazz.getName());
}
//Adding mapping
classPropertyDirectory.addMapping(clazz, propertyName, result);
}
}
accessors.decrementAndGet();
return result;
}
public void flushDirectory(final ClassLoader cl) {
//We wait until all getPropertyDescriptor() calls in progress have completed.
synchronized(flushingLock) {
while(accessors.intValue() > 0) {
try {
Thread.sleep(100);
} catch(final InterruptedException e) {
//No show stopper
}
}
for(final Iterator<Class<?>> it =
classPropertyDirectory.classMap.keySet().iterator(); it.hasNext();) {
final Class<?> clazz = it.next();
if(clazz.getClassLoader().equals(cl)) {
it.remove();
Introspector.flushFromCaches(clazz);
}
}
}
}
//The rest of the inner classes are omitted...
}
导入java.beans.BeanInfo;
导入java.beans.IntrospectionException;
导入java.beans.Introspector;
导入java.beans.PropertyDescriptor;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入java.util.concurrent.AtomicInteger;
公共类描述符目录{
private final ClassPropertyDirectory ClassPropertyDirectory=new ClassPropertyDirectory();
私有最终对象flushingLock=新对象();
私有最终AtomicInteger访问器=新的AtomicInteger(0);
公共描述符目录(){}
公共PropertyDescriptor getPropertyDescriptor(最终类clazz,最终字符串propertyName)引发异常{
//首先递增访问器计数。
已同步(flushingLock){
accessors.incrementAndGet();
}
属性描述器结果;
//在目录类根目录上同步
//这比完全方法同步更可取,因为两次查找
//不同的类永远不能在同一个目录路径上,也不会发生冲突
同步(clazz){
结果=classPropertyDirectory.getPropertyDescriptor(clazz,propertyName);
如果(结果==null){
//尚未加载PropertyDescriptor
//首先,我们需要关于父类的bean信息
最终BeanInfo BeanInfo;
试一试{
beanInfo=内省者。getBeanInfo(clazz);
}捕获(最终内省异常e){
accessors.decrementAndGet();
投掷e;
//TODO:抛出特定的
}
//现在我们必须找到目标属性的PropertyDescriptor
final PropertyDescriptor[]propList=beanInfo.getPropertyDescriptors();
对于(inti=0;(i0){
试一试{
睡眠(100);
}捕获(最终中断异常e){
//不显示停止
}
}
对于(最终迭代器,据我所知,您可以在此处使用:
private ReadWriteLock lock=new ReentrantReadWriteLock();
私有锁readLock=Lock.readLock();
私有锁writeLock=Lock.writeLock();
公共PropertyDescriptor getPropertyDescriptor(最终类clazz,最终字符串propertyName)引发异常{
private ReadWriteLock lock = new ReentrantReadWriteLock();
private Lock readLock = lock.readLock();
private Lock writeLock = lock.writeLock();
public PropertyDescriptor getPropertyDescriptor(final Class<?> clazz, final String propertyName) throws Exception {
readLock.lock();
try {
...
} finally {
readLock.unlock();
}
}
public void flushDirectory(final ClassLoader cl) {
writeLock.lock();
try {
...
} finally {
writeLock.unlock();
}
}