在并发访问List时,以下代码是线程安全的吗?
挥发性限定在这里添加任何值吗?使用布尔值进行同步
class concurrentList{
private AtomicBoolean locked = new AtomicBoolean(true);
volatile List<Integer> list=new LinkedList<Integer>();
long start = System.currentTimeMillis();
long end = start + 60*100;
public void push(int e){
while(!locked.get());
list.add(e);
while(!locked.compareAndSet(true,false));
}
public int pop(){
int elem;
while(locked.get());
elem=(Integer)list.remove(0);
while(!locked.compareAndSet(false,true));
return elem;
}
....
}
不确定为什么'push'和'pop'方法有不同的锁定条件。但是从这一点开始,为什么不使用['java.util.concurrent.Lock'](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html ?is-external = true)对象? – n247s