MediumOne / checkthread

Automatically exported from code.google.com/p/checkthread
0 stars 0 forks source link

Feature Suggestion - Should warn when Lock not released in a finally #18

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.checkthread.annotations.ThreadSafe;

public class SomeClass {

    private final List<String> list = new ArrayList<String>();
    private final ReadWriteLock lock = new ReentrantReadWriteLock();

    @ThreadSafe
    public void add(String obj){
        lock.writeLock().lock();
        list.add(obj);
        lock.writeLock().unlock();
    }

}

This should generate a warning that the unlock isn't in a finally:

    @ThreadSafe
    public void add(String obj){
        lock.writeLock().lock();
        try {
            list.add(obj);
        } finally {
            lock.writeLock().unlock();
        }
    }

Original issue reported on code.google.com by pas...@gmail.com on 10 Jan 2011 at 2:41