使用java Lock和Condition对象

时间:2014-12-11 14:54:05

标签: java multithreading synchronization locking synchronized

我在下面构建了这个数据库对象。

我希望方法更新和查询为: 1.以多线程方式工作(意味着2个线程可以同时访问它们 - 同步防止 - 这就是为什么我不能将它添加到mehtod singutare)。 2.如果一个线程已经开始打开数据库而第二个线程(在它之后启动)试图使用该数据库 - 我不想得到第二个线程在尚未打开时访问数据库的情况。换句话说 - 我希望它在一般情况下是多线程的,但是当1个线程第一次打开数据库时,线程安全(原子)。

谢谢, 下面的类是我需要插入逻辑的地方(更具体地说 - 是查询和更新方法)。

public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;

    private SingeltonDB(String username, String password) {
        db = new DBconnImpl();
    }

    public static boolean isOpen() {
        return (db != null);
    }

    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {
        if (db != null) {
            throw (new Exception("The database is  open"));
        } else {
            System.out.println("The database is now open");
            singalDb = new SingeltonDB(username, password);
        }
        db.connect(username, password);
        System.out.println("The database was connected");
        return singalDb;
    }

    public synchronized static SingeltonDB getInstance() throws Exception {
        if (db == null) {
            throw (new Exception("The database is not open"));
        }

        return singalDb;
    }

    public void create(String tableName) throws Exception {
        db.create(tableName);
    }

    public  User query(String tableName, int rowID) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return null;
        }
        return (db.query(tableName, rowID));
    }

    public  void update(String tableName, User user) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return;
        }
        db.update(tableName, user);
    }

}

1 个答案:

答案 0 :(得分:1)

我会将连接调用从getInstance静态方法移动到SingeltonDB构造函数。这将保证每当您获得对静态db字段的引用时都会打开db。我还要将db == null检查添加到所有非静态方法。

public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;

    private SingeltonDB(String username, String password) {
        db = new DBconnImpl();
        db.connect(username, password);
        System.out.println("The database was connected");
    }

    public static boolean isOpen() {
        return (db != null);
    }

    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {
        if (db != null) {
            throw (new Exception("The database is  open"));
        } else {
            System.out.println("The database is now open");
            singalDb = new SingeltonDB(username, password);
        }
        return singalDb;
    }

    public synchronized static SingeltonDB getInstance() throws Exception {
        if (db == null) {
            throw (new Exception("The database is not open"));
        }

        return singalDb;
    }

    private static void checkDbOpened() throws Exception {
        if (db == null) {
            throw new Exception("The database is not open");
        }
    }

    public void create(String tableName) throws Exception {
        checkDbOpened();
        db.create(tableName);
    }

    public  User query(String tableName, int rowID) throws Exception {
        checkDbOpened();
        return (db.query(tableName, rowID));
    }

    public  void update(String tableName, User user) throws Exception {
        checkDbOpened();
        db.update(tableName, user);
    }

}

这是一个更新的单例,可以让您确定是否创建了表

public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;
    private static ConcurrentSkipListSet<String> tableNames = new ConcurrentSkipListSet<String>();

    private SingeltonDB(String username, String password) {
        db = new DBconnImpl();
        db.connect(username, password);
        System.out.println("The database was connected");
    }

    public static boolean isOpen() {
        return (db != null);
    }

    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {
        if (db != null) {
            throw (new Exception("The database is  open"));
        } else {
            System.out.println("The database is now open");
            singalDb = new SingeltonDB(username, password);
        }
        return singalDb;
    }

    public synchronized static SingeltonDB getInstance() throws Exception {
        if (db == null) {
            throw (new Exception("The database is not open"));
        }

        return singalDb;
    }

    private static void checkDbOpened() throws Exception {
        if (db == null) {
            throw new Exception("The database is not open");
        }
    }

    private static void checkForTable(String tableName) {
        if (tableNames.add(tableName)) {
           db.create(tableName);
        }
    }

    public void create(String tableName) throws Exception {
        checkDbOpened();
        checkForTable(tableName);
    }

    public  User query(String tableName, int rowID) throws Exception {
        checkDbOpened();
        checkForTable(tableName);
        return (db.query(tableName, rowID));
    }

    public  void update(String tableName, User user) throws Exception {
        checkDbOpened();
        checkForTable(tableName);
        db.update(tableName, user);
    }

}

checkForTable函数将确定是否已创建给定的tableName。如果它不是,它将创建表。此更新将确保表在使用之前已创建。此代码的问题在于它不能跨进程工作,但只能在单个进程中工作,除非db类知道如何管理跨进程边界创建的表。