1
+ /*
2
+ * Copyright (C) 2011-2015 Markus Junginger, greenrobot (http://greenrobot.de)
3
+ *
4
+ * This file is part of greenDAO Generator.
5
+ *
6
+ * greenDAO Generator is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ * greenDAO Generator is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with greenDAO Generator. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ package de .greenrobot .daotest ;
19
+
20
+ import java .util .ArrayList ;
21
+ import java .util .List ;
22
+
23
+ import de .greenrobot .dao .test .AbstractDaoSessionTest ;
24
+
25
+ public class DeadlockPreventionTest extends AbstractDaoSessionTest <DaoMaster , DaoSession > {
26
+
27
+ volatile boolean done ;
28
+ private TestEntityDao dao ;
29
+
30
+ public DeadlockPreventionTest () {
31
+ super (DaoMaster .class );
32
+ }
33
+
34
+ // Runs pretty long, only run manually
35
+ public void _testLoadAll () throws InterruptedException {
36
+ dao = daoSession .getTestEntityDao ();
37
+ List <TestEntity > entities = new ArrayList <>();
38
+ for (int i = 0 ; i < 10000 ; i ++) {
39
+ TestEntity entity = new TestEntity ();
40
+ entity .setSimpleStringNotNull ("Text" + i );
41
+ entities .add (entity );
42
+ }
43
+ dao .insertInTx (entities );
44
+
45
+ System .out .println ("Entities inserted" );
46
+ Thread thread = new InsertThread ();
47
+ Thread thread2 = new InsertBatchThread ();
48
+ thread .start ();
49
+ thread2 .start ();
50
+
51
+ for (int i = 0 ; i < 10 ; i ++) {
52
+ System .out .println ("Starting loadAll #" + i );
53
+ dao .loadAll ();
54
+ }
55
+
56
+ done = true ;
57
+ thread .join ();
58
+ thread2 .join ();
59
+ }
60
+
61
+ private class InsertThread extends Thread {
62
+ @ Override
63
+ public void run () {
64
+ int counter = 0 ;
65
+ while (!done ) {
66
+ TestEntity entity = new TestEntity ();
67
+ entity .setSimpleStringNotNull ("TextThread" + counter );
68
+ dao .insert (entity );
69
+ counter ++;
70
+ if (counter % 10 == 0 ) {
71
+ System .out .println ("Thread inserted " + counter );
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ private class InsertBatchThread extends Thread {
78
+ @ Override
79
+ public void run () {
80
+ int counter = 0 ;
81
+ List <TestEntity > batch = new ArrayList <>();
82
+ while (!done ) {
83
+ TestEntity entity = new TestEntity ();
84
+ entity .setSimpleStringNotNull ("TextThreadBatch" + counter );
85
+ batch .add (entity );
86
+ counter ++;
87
+ if (counter % 10 == 0 ) {
88
+ dao .insertInTx (batch );
89
+ System .out .println ("Batch Thread inserted " + counter );
90
+ batch .clear ();
91
+ }
92
+ }
93
+ }
94
+ }
95
+ }
0 commit comments