Android中Sqlite數據庫多線程併發問題
2011-07-14 00:09 by 清炒白菜, 1793 閱讀, 2 評論, 收藏, 編輯最近在做一個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 保證鎖對象不會被修改
測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
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()是爲了測試自旋鎖
Android中Sqlite數據庫多線程併發問題
2011-07-14 00:09 by 清炒白菜, 1793 閱讀, 2 評論, 收藏, 編輯最近在做一個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 保證鎖對象不會被修改
測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
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()是爲了測試自旋鎖