首次调用SQLiteCursor的getCount()需要锁定数据库

当我们第一调用android.database.sqlite.SQLiteCursorgetCount()时,当前线程会锁定数据库,在该操作完成后才解锁。
其调用关系如下
at android.database.sqlite.SQLiteQuery.native_fill_window(Native Method) 
at android.database.sqlite.SQLiteQuery. fillWindow( SQLiteQuery.java:73) 
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:287) 
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:268) 
at android.widget.CursorAdapter. getCount (CursorAdapter.java:132) 
如果是第一次调用 SQLiteCursor getCount () 的话,在getCount()中,它会调用 fillWindow (),
在SQLiteCursor的 fillWindow() 中,它又会调用SQLiteQuery fillWindow()
android.database.sqlite.SQLiteCursor 的相关 源码如下:
@Override
    public int getCount() {
        if (mCount == NO_COUNT) {
            fillWindow(0);
        }
        return mCount;
    }

    private void fillWindow ( int startPos) {
        if (mWindow == null) {
            // If there isn't a window set already it will only be accessed locally
            mWindow = new CursorWindow(true /* the window is local only */);
        } else {
            mCursorState++;
                queryThreadLock();
                try {
                    mWindow.clear();
                } finally {
                    queryThreadUnlock();
                }
        }
        mWindow.setStartPosition(startPos);
        mCount = mQuery.fillWindow(mWindow, mInitialRead, 0);
        // return -1 means not finished
        if (mCount == NO_COUNT){
            mCount = startPos + mInitialRead;
            Thread t = new Thread(new QueryThread(mCursorState), "query thread");
            t.start();
        } 
    }
SQLiteQuery fillWindow() 中,它首先需要 lock数据库 ,然后调用JNI层的 native_fill_window() 进行数据库操作,在其操作完成之后才 unlock数据库
android.database.sqlite.SQLiteQuery 的相关源码如下:
/**
     * Reads rows into a buffer. This method acquires the database lock.
     *
     * @param window The window to fill into
     * @return number of total rows in the query
     */
    int fillWindow(CursorWindow window,
             int maxRead,  int lastPos) {
         long timeStart = SystemClock.uptimeMillis();
         mDatabase.lock();
        mDatabase.logTimeStat(mSql, timeStart, SQLiteDatabase.GET_LOCK_LOG_PREFIX);
         try {
            acquireReference();
             try {
                window.acquireReference();
                // if the start pos is not equal to 0, then most likely window is
                // too small for the data set, loading by another thread
                // is not safe in this situation. the native code will ignore maxRead
                 int numRows =  native_fill_window(window, window.getStartPosition(), mOffsetIndex,
                        maxRead, lastPos);

                // Logging
                 if (SQLiteDebug.DEBUG_SQL_STATEMENTS) {
                    Log.d(TAG, "fillWindow(): " + mSql);
                }
                mDatabase.logTimeStat(mSql, timeStart);
                return numRows;
            }  catch (IllegalStateException e){
                // simply ignore it
                return 0;
            }  catch (SQLiteDatabaseCorruptException e) {
                mDatabase.onCorruption();
                throw e;
            }  finally {
                window.releaseReference();
            }
        } finally {
            releaseReference();
             mDatabase.unlock();
        }
    }

结束!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值