James James - 9 months ago 47
Android Question

Proper use of yieldIfContendedSafely() in an Android Multithreaded Application

In my application, I am using an

to write some data to a database in a transaction. This database is also accessed from the UI thread. While looking through the available database methods, I came across
. It seems like this method should be used for any cases where a transaction is being made from a separate thread. But there is hardly any documentation on this method other than the following:

Temporarily end the transaction to let other threads run. The transaction is assumed to be successful so far. Do not call
before calling this. When this returns a new transaction will have been created but not marked as successful. This assumes that there are no nested transactions (
has only been called once) and will throw an exception if that is not the case.

Here is how I would assume that you would use this method from a thread:

try {
//insert some stuff into the database here

// is this how you use this method?
boolean yielded = db.yieldIfContendedSafely();
if (yielded) {
try {
} catch (InterruptedException e) {

} catch (SQLException e) {
return false;
} finally {

Is this the correct way to use this method? Is it alright to use
more than once in the same transaction, in between multiple writes to different tables in the database? Any suggestions?

Answer Source

Pulling some example code from the Android libraries it seems much simpler to use than that...

This is taken from com.android.providers.calendar.SQLiteContentProvider.java

public int bulkInsert(Uri uri, ContentValues[] values) {
    int numValues = values.length;
    mDb = mOpenHelper.getWritableDatabase();
    try {
        for (int i = 0; i < numValues; i++) {
            Uri result = insertInTransaction(uri, values[i]);
            if (result != null) {
                mNotifyChange = true;
    } finally {

    return numValues;

Also looking into the source code for the function itself, it seems that, if yielded, the call will defer execution of your thread for a short period in any case.