如何执行并行线程,并没有插入数据库锁相应的SQLite数据库的数据? [英] How to Execute threads in parallel and insert corresponding data in sqlite database without database lock?

查看:526
本文介绍了如何执行并行线程,并没有插入数据库锁相应的SQLite数据库的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用多个API在单独的线程执行的每个并不会造成锁插入来自回应SQLite数据库相应的数据。谁能帮我在这方面有一个工作的例子,我可以参考一下。


解决方案

  

我需要调用多个API在单独的线程执行的每个并不会造成锁插入来自回应SQLite数据库相应的数据。


您不需要做什么特别的。多个线程可以使用同一个 SQLiteDatabase 对象无需你做在应用程序级锁。 SQLite的确实在幕后它自己的锁。你永远不应该得到死锁,但一个线程必须等待另一个线程完成使得其插入。

请参阅这些问题/回答:


  • <一个href=\"http://stackoverflow.com/questions/8446585/can-two-asynctasks-share-the-same-sqlitedatabase-object/8446624#8446624\">Can 2 AsyncTasks共享同一个SQLiteDatabase对象?


  • <一个href=\"http://stackoverflow.com/questions/2493331/what-are-the-best-practices-for-sqlite-on-android/3689883#3689883\">What对于SQLite的最佳做法在Android?


Android的下SQLite是单线程的。即使多个线程使用相同的数据库连接,我的理解是,他们将在同时运行被阻止。有没有办法来解决这个限制。如果打开到同一个数据库的两个连接,这会破坏数据库,因为数据库的更新将不会进行协调。

I need to call multiple api's each executing in separate thread and insert the respective data from response to sqlite database without causing locks. Can anyone help me in this regard with a working example that I can refer to.

解决方案

I need to call multiple api's each executing in separate thread and insert the respective data from response to sqlite database without causing locks.

You don't need to do anything special. Multiple threads can make use of the same SQLiteDatabase object without you doing locking at the application level. Sqlite does it's own locking under the covers. You should never get deadlocks but one thread will have to wait for the other thread to finish making its insert.

See these questions/answers:

Sqlite under Android is single threaded. Even if multiple threads were using the same database connection, my understanding is that they would be blocked from running concurrently. There is no way to get around this limitation. If you open two connections to the same database, this would corrupt the database because database updates would not be coordinated.

这篇关于如何执行并行线程,并没有插入数据库锁相应的SQLite数据库的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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