简体   繁体   English

我需要同步吗

[英]Do I need Synchronize this

I want to make my Database operation in a spread thread, so first I make a ThreadLooper , which will be used to post Runnables , that are starting DB operations. 我想在一个传播线程中执行数据库操作,因此首先创建一个ThreadLooper ,它将用于发布Runnables ,它们正在启动数据库操作。

It looks like this: 看起来像这样:

import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;

/**
 * @author  
 * @version 1.0 This class is used as ThreadLooper to make the database
 *          operation CRUD , this looper is singlton across the app
 * 
 */
public class DBThreadLooper extends HandlerThread {
    public Handler mHandler;

    private DBThreadLooper(String name) {
        super(name);

    }

    private static DBThreadLooper mInstance;

    public static DBThreadLooper newInstance() {

        if (mInstance == null) {
            mInstance = new DBThreadLooper("DATA BASE THREAD LOOPER ");
            mInstance.start();
        }
        return mInstance;
    }

    @Override
    public synchronized void start() {
        super.start();
        waitUntilReady();
    }

    private void waitUntilReady() {
        mHandler = new Handler(getLooper(), new Callback() {

            public boolean handleMessage(Message msg) {

                return true;
            }
        });
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

}

Now I have this method that will make a DB operation 现在我有这种方法可以进行数据库操作

private void handleFavButton() {
        int index = viewPager.getCurrentItem();
        Cursor c = mAdapter.getAdapterCursor();
        c.moveToPosition(index);
        final String  quote_id = c.getString(c.getColumnIndex(QuoteTableMetaData._ID));

        final int is_fav = c.getInt(c.getColumnIndex(QuoteTableMetaData.C_IS_FAVORITE));


        if(is_fav == 0){
            DBThreadLooper looper = DBThreadLooper.newInstance();
            looper.mHandler.post(new Runnable() {

                public void run() {
                    //1. make it 1 
                    QuoteTableMetaData qTable = QuoteTableMetaData
                            .getInstance();
                    ContentValues values = new ContentValues();
                    values.put(QuoteTableMetaData.C_IS_FAVORITE, new Integer(1));
                    qTable.update(DBUtils.getDBHelper(getApplicationContext())
                            .getWritableDatabase(), values,
                            QuoteTableMetaData._ID + "= ?",
                            new String[] { quote_id });
                    //2. insert a new record in Fav Table with the id 
                    FavouriteQuoteTable fTable = FavouriteQuoteTable
                            .getInstance();
                    values.clear();
                    values.put(FavouriteQuoteTable.C_QUOTE_ID, quote_id);
                    fTable.insert(DBUtils.getDBHelper(getApplicationContext())
                            .getWritableDatabase(), null, values);
                }
            });
        }
        else{
            DBThreadLooper looper = DBThreadLooper.newInstance();
            looper.mHandler.post(new Runnable() {

                public void run() {
                    //1.make it 0 
                    QuoteTableMetaData qTable = QuoteTableMetaData
                            .getInstance();
                    ContentValues values = new ContentValues();
                    values.put(QuoteTableMetaData.C_IS_FAVORITE, new Integer(0));
                    qTable.update(DBUtils.getDBHelper(getApplicationContext())
                            .getWritableDatabase(), values,
                            QuoteTableMetaData._ID + "=?",
                            new String[] { quote_id });
                    // 2. delete record with id from fav Table 
                    FavouriteQuoteTable fTable = FavouriteQuoteTable
                            .getInstance();
                    fTable.delete(DBUtils.getDBHelper(getApplicationContext())
                            .getWritableDatabase(),
                            FavouriteQuoteTable.C_QUOTE_ID + "=?",
                            new String[] { quote_id });
                }
            });

        }

Do I need to make quote_id and is_fav in the method volatile , so that my method will not run into synchronization problems. 我是否需要使方法中的quote_idis_fav volatile ,以便我的方法不会遇到同步问题。

No mutlithread problem with them: they are local variables (furthermore final). 它们没有mutlithread问题:它们是局部变量(而且是最终变量)。 This means that every call to the method handleFavButton will have separate instances of them and the different calls accessing the variables will not interfere. 这意味着对方法handleFavButton每次调用handleFavButton将具有它们的单独实例,并且访问变量的不同调用不会受到干扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM