我有一个要求如下:
1) 具有所有静态方法和静态列表的类。这个列表存储了一些我对其执行一些操作的对象。
2) 现在这个操作是从多个线程调用的。
3) 此操作调用不共享任何公共数据,因此此方法不同步。
4) 现在,每当这个列表用新对象更新时,我必须停止这个操作调用。
class StaticClass{
static List<SomeObjects>=new List<>();
static performOperation()
{
//call operation on all objects in the list
}
static updateList()
{
//update list, add, update or remove objects
}
}
可能的解决方案 :
1) 使 performOperation() 和 updateList() 同步。但是 performOperation() 被调用的频率太高,而更新列表频率太低。
2)使用读写锁。在 performOperation() 中使用读锁,在 updateList() 中使用写锁。示例如下所示:
class StaticClass{
static List<SomeObjects>=new List<>();
static final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
static performOperation()
{
readWriteLock.readLock().lock();
//call operation on all objects in the list
readWriteLock.readLock().unlock();
}
static updateList()
{
readWriteLock.writeLock().lock();
//update list, add, update or remove objects
readWriteLock.writeLock().unlock();
}
那么哪种解决方案更好呢?这是读写锁的正确用法吗?为什么我对方法 2 感到困惑是 performOperation() 中没有需要读访问或写访问的数据。我只是在更新列表时无法调用此方法。所以我不确定它是否适合使用读写锁。