【发布时间】:2015-03-13 05:25:10
【问题描述】:
我正在寻找类似于这种语法的东西,即使它不存在。
我想让一个方法作用于一个集合,并且在该方法的生命周期内,确保集合不会被弄乱。
所以它可能看起来像:
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
}
}
这是最好的方法吗?
【问题讨论】:
-
除非我误解了这个问题,否则这里唯一的区别是缩进级别。
标签: java thread-synchronization java-collections-api