vendredi 25 mars 2016

Does Android SQLiteDatabase thread-safe?

Many people said that SQLiteDatabase is thread-safe,but ,when i ran some simple tests with setup like this:

private class MyRunnable implements Runnable{

    @Override
    public void run() {
        for(int i = 0 ;i < 100 ; i++){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Cursor cursor = database.query(true,"testdb",new String[]{"num"},"id=?",
                    new String[]{"1"},null,null,null,null);
            cursor.moveToFirst();
            int original = cursor.getInt(0);
            ContentValues values = new ContentValues();
            values.put("num",++original);
            database.update("testdb",values,"id=?",new String[]{"1"});
        }
    }
}

MyRunnable runnable1 = new MyRunnable();
MyRunnable runnable2 = new MyRunnable();

new Thread(runnable1).start();
new Thread(runnable2).start();

In MyRunnable ,there is a loop that runs 100 times. Each time, the num field will add by 1 and the num's initial value is 0. we can see that the above code's expected result is 200,but i only get 197.

so does that mean SQLiteDatabase is not thread-safe?

Aucun commentaire:

Enregistrer un commentaire