Questions tagged «synchronize»

8
如果可以使用同步的(this),为什么还要使用ReentrantLock?
我试图了解是什么使并发锁如此重要,如果可以使用的话synchronized (this)。在下面的虚拟代码中,我可以执行以下任一操作: 同步了整个方法或同步了易受攻击的区域(synchronized(this){...}) 或使用ReentrantLock锁定易受攻击的代码区域。 码: private final ReentrantLock lock = new ReentrantLock(); private static List<Integer> ints; public Integer getResult(String name) { . . . lock.lock(); try { if (ints.size()==3) { ints=null; return -9; } for (int x=0; x<ints.size(); x++) { System.out.println("["+name+"] "+x+"/"+ints.size()+". values >>>>"+ints.get(x)); } } finally { lock.unlock(); } …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.