如何同步访问的SQLite数据库前景和背景之间的流程? [英] How to synchronize access to sqlite db between foreground and background process?

查看:146
本文介绍了如何同步访问的SQLite数据库前景和背景之间的流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在其中可以访问(读取和写入),以SQLite数据库后台运行的服务。在我的Andr​​oid应用程序对应的前台进程(主要的应用程序)也可访问(读取和写入),以SQLite数据库。

I have a service that runs in the background which can access (read and write) to sqlite database. The corresponding foreground process (main app) can also access (read and write) to sqlite database in my android application.

如何与试图访问prevent问题/保存/在同一时间读取/从sqlite的分贝应该是什么这样做的正确方法?

How can i prevent problems related to trying to access / save / read at the same time to/from sqlite db and what should be the correct way to do this?

推荐答案

有2个简单的方法可以做到这一点,因为ChirstopheCVB写第一:创建一个同步的方法。一个synchronized方法只能由一个线程访问,其​​他线程被阻塞。

There are 2 easy ways to do this, the first as ChirstopheCVB writes: Create a synchronized method. A synchronized method can only be accessed by one thread, other threads are blocked.

这是建议的方法,因为它保持在一个地方所有的数据库访问相关的code。当你改变你的数据库的表布局,你并不需要通过所有code的追捕,并做出调整。你隐藏从你的主线程的详细信息。

This is the recommended method, as it keeps all DB access related code in a single place. When you ever change the table layout of your database, you do not need to hunt through all of your code and make adjustments. You 'hide' the details from your main thread.

如果你想保持在主线程的分贝code,然后用信号灯。还有是在Java的支持:
http://developer.android.com/reference/java/util/concurrent /Semaphore.html

If you want to keep your db code in the main thread, then use semaphores. There is support for that in Java: http://developer.android.com/reference/java/util/concurrent/Semaphore.html

一个信号量(简体)表示,如果有人访问共享资源(例如你DB)的单个对象。在访问数据库之前,请检查您是否允许,如果你是,那么设置对象使其他人块。做你的事,并重置对象,以便其他人可以尝试访问该资源。

A semaphore is (simplified) a single object indicating if somebody is accessing a shared resource (e.g. your DB). Before accessing the DB, check if you are allowed and if you are, then set the object so it blocks others. Do your thing and reset the objects so others can try to access the resource.

有一个简单的例子,假设你有线程A和线程B,以及一个全局变量canUse

A simple example, assume you have thread A & thread B, and a global variable canUse.

Init: canUse = true;

Thread A:
while (!canUse) wait;   // busy waiting, your thread is blocked
canUse = false;         // canUse was true, now we grab access.
do our thing;
canUse = true;          // Others are allowed to use the resource again.

线程B看起来是一样的。

Thread B looks the same.

这工作,但有2个问题。首先,你是阻止您的线程,因为你正在等待资源可用。是有风险的,因为它可能永远不会成为可用,你有死锁。

This works, but there are 2 problems. First, you are blocking your thread because you are waiting for the resource to become available. There is a risk, because it might never become available and you have deadlock.

第二个问题更为严重。想象一下,你有3个线程:A,B和C C已经抓住了锁,这两个A和B的等待。以下是可能的。

The second problem is more severe. Imagine you have 3 threads: A, B and C. C has grabbed the lock and both A & B are waiting. The following is possible

A: while (!canUse) wait;
B: while (!canUse) wait;
C: canUse = true;
A: Hooray, we can grab the resource
B: Hooray, we can grab the resource
A: canUse = false;
B: canUse = false;

上面的例子表明它是如何重要的是检查变量和改变是在原子的方式完成。换句话说,没有别的可能发生。

Above example shows how important it is that checking the variable and changing it is done in an atomic way. In other words, nothing else can happen.

幸运的是,Java提供您信号灯。这是不容易在一开始理解,但必须明白,如果你想要做你所要求的东西,而不使用synchronized方法(这些方法作为信号灯为您无需额外的编码工作)。

Fortunately, java provides you with semaphores. It is not easy to understand in the beginning, but a must to understand if you want to do what you are asking for without using synchronized methods (such methods act as semaphores for you without extra coding effort).

这篇关于如何同步访问的SQLite数据库前景和背景之间的流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆