java - 我需要同步这个吗

标签 java android multithreading synchronization

我想在扩展线程中进行数据库操作,因此首先我创建一个 ThreadLooper,它将用于发布启动数据库操作的 Runnables

看起来像这样:

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();
    }

}

现在我有这个方法可以进行数据库操作

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 });
                }
            });

        }

我是否需要在方法 volatile 中制作quote_idis_fav,这样我的方法就不会遇到同步问题。

最佳答案

它们没有多线程问题:它们是局部变量(而且是最终变量)。这意味着对方法 handleFavButton 的每次调用都会有它们的单独实例,并且访问变量的不同调用不会干扰。

关于java - 我需要同步这个吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10460727/

相关文章:

android - 载入画面介绍

c# - 哪些方法可以让线程等待一个事件然后继续执行?

java - 在android中使用kSOAP连接到web服务

java - 带邻域搜索的子集总和 - java

android - Bluetooth LE 外围设备在与 Bluetooth LE 中央设备连接时停止广告

java - Android:context.getDrawable() 的替代方案

linux - 有多少个进程可以在后台并行运行

java - 对 XYChart 系列数据进行排序

java - JTable全表数据更改

java - EditText 变量未在另一个类中分配