Java return 与finally


public Map<String, Set<NearFinderData>> getDataMap() {
        Map<String, Set<NearFinderData>> latestData = null;
        lock.readLock().lock();
        try {
            latestData = NEAR_FINDER_DATA;
        } finally {
            lock.readLock().unlock();
        }
        return latestData;
    }

public Map<String, Set<NearFinderData>> getDataMap() {
        lock.readLock().lock();
        try {
            return NEAR_FINDER_DATA;
        } finally {
            lock.readLock().unlock();
        }

    }

的区别是什么呢?其中NEAR_FINDER_DATA 是一个被修改的Map

java read-write-lock finally lock return

Rainbow 11 years, 4 months ago

Java的finally在任何情况下都会被调用,System.exit()除外。
所以,两个区别就是一个先解锁后返回,一个先返回后解锁

生鲜实验室 answered 11 years, 4 months ago

Your Answer