我正在寻找类似于这种语法的东西,即使它不存在。
我想让一个方法作用于一个集合,并且在该方法的生命周期内,确保集合不会被弄乱。
所以这可能看起来像:
private void synchronized(collectionX) doSomethingWithCollectionX() {
// do something with collection x here, method acquires and releases lock on
// collectionX automatically before and after the method is called
}
但是,恐怕这样做的唯一方法是:
private void doSomethingWithTheCollectionX(List<?> collectionX) {
synchronized(collectionX) {
// do something with collection x here
}
}
这是最好的方法吗?