惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - 清炒白菜

Unable to resume activity : android.database.StaleDataException: Attempted to access a cursor after it has been closed. 异常 Android的匿名Handler类引起的内存泄露 计算2个经纬度之间的距离 用bcp导入DateTime类型的数据 Linux下提示“omitting directory”错误的解决办法 Android的"返回“功能 EditText获得焦点后,如何关闭软键盘 获取当前Activity的Root View 用代码动态设置ImageView的align布局 [转]Java的数组(Array)、Vector、ArrayList、HashMap的异同 HTML中element.style取值问题 用bcp导入大量数据(代替INSERT) ajax跨域访问问题 又一款分布式版本控制工具Mercurial bcp导入导出数据发生异常解决方案 - 清炒白菜 - 博客园 导出Google Reader中加星的内容项 - 清炒白菜 - 博客园 在win下使用GIT dotNet 自带线程池与HTTP访问的若干疑问 解决"the database principal owns a schema in the database and cannot be dropped"问题
Android中Sqlite数据库多线程并发问题
清炒白菜 · 2011-07-14 · via 博客园 - 清炒白菜

2011-07-14 00:09  清炒白菜  阅读(6220)  评论()    收藏  举报

最近在做一个Android项目, 为了改善用户体验,把原先必须让用户“等待”的过程改成在新线程中异步执行。但是这样做遇到了多个线程同时需要写Sqlite数据库,导致操作数据库失败。

本人对JAVA并不熟悉,只接触了2个多月(纯粹为了Android项目, 才开始接触JAVA), 在网上搜了一下, 发现JAVA在线程同步方面提供的功能相对于C#真少,只找到synchronized关键字, 而且还不提供timeout机制, 不过经过测试, 至少解决了目前的需求。

问题需求:

1. Android中, 对Sqlite数据库操作, 需要自己实现一个继承自SQLiteOpenHelper的类

2. 实现类中, 对数据库read操作不需要同步(提高效率)

3. 实现类中, 所有对数据库写操作, 必须要同步

解决方法:

定义一个private final static byte[] _writeLock = new byte[0]; 作为写锁

static  保证多个实例之间, 使用的是同一个对象(同一把锁)

final 保证锁对象不会被修改

测试代码:

package com.yushiro;


public class multiThreadTest {

	private final static byte[] _writeLock = new byte[0];
	private static int _syncInt = 0;
	public void Method1(){
		synchronized (_writeLock) {
			StringBuffer sb = new StringBuffer();
			sb.append(Thread.currentThread().getName());
			sb.append(" Method1 ");
			sb.append(_syncInt);
			_syncInt++;
			sb.append(" --> ");
			sb.append(_syncInt);
			System.out.println(sb.toString());
		}
	}
	
	public void Method2(){
		synchronized (_writeLock) {
			StringBuffer sb = new StringBuffer();
			sb.append(Thread.currentThread().getName());
			sb.append(" Method2 ");
			sb.append(_syncInt);
			_syncInt++;
			sb.append(" --> ");
			sb.append(_syncInt);
			System.out.println(sb.toString());		}
	}
	
	public void Method3(){
		synchronized (_writeLock) {
			this.Method1();
			this.Method2();
			}
	}
}
package com.yushiro;


public class MainTest {

	
	public static void main(String[] args) {
		
		Thread t2= new Thread1();
		Thread t1= new Thread2();
		Thread t3 = new Thread3();
		t1.start();
		t2.start();
		t3.start();
	}

	private static class Thread2 extends Thread {
		public void run() {
			multiThreadTest tmp = new multiThreadTest();
			while(true) {
				
				
				try {
					Thread.sleep(30000);
					tmp.Method1();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//System.out.println(y);
			}
		}
	}
	
	private static class Thread1 extends Thread {
		public void run() {
			multiThreadTest tmp = new multiThreadTest();
			while(true) {
				
				
				try {
					Thread.sleep(100);
					tmp.Method2();
					Thread.sleep(100);
					tmp.Method1();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//System.out.println(y);
			}
		}
	}
	private static class Thread3 extends Thread {
		public void run() {
			multiThreadTest tmp = new multiThreadTest();
			while(true) {
				
				
				try {
					Thread.sleep(100);
					tmp.Method3();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//System.out.println(y);
			}
		}
	}

}

Method3()是为了测试自旋锁

参考:

Java同步机制总结--synchronized(http://leo-faith.iteye.com/blog/177779)