本文只是对GreenDao数据库多线程读写操作的验证:
package com.windfallsheng.mygreendao;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import org.greenrobot.greendao.AbstractDaoMaster;
import org.greenrobot.greendao.AbstractDaoSession;
import org.greenrobot.greendao.database.DatabaseOpenHelper;
import org.greenrobot.greendao.query.Query;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
public class MainActivity extends AppCompatActivity {
UserDao mUserDao;
private AtomicInteger num = new AtomicInteger(0);
private AtomicInteger task = new AtomicInteger(0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseOpenHelper helper = new DaoMaster.DevOpenHelper(this, "user-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
AbstractDaoMaster daoMaster = new DaoMaster(db);
AbstractDaoSession daoSession = daoMaster.newSession();
mUserDao = (UserDao) daoSession.getDao(User.class);
}
public void add(View view) {
// AddCountDownTimer addCountDownTimer = new AddCountDownTimer(10 * 1000, 200);
// addCountDownTimer.start();
new Thread(() -> {
for (int i = 0; i < 10000; i++) {
int j = num.incrementAndGet();
User user = new User("name" + "_" + j);
Log.d("MainActivity", "addInThread#1#user=" + user);
mUserDao.insert(user);
}
int taskNum = task.incrementAndGet();
Log.d("MainActivity", "addInThread_1_#taskNum=" + taskNum);
}).start();
new Thread(() -> {
for (int i = 10000; i < 20000; i++) {
int j = num.incrementAndGet();
User user = new User("name" + "_" + j);
Log.d("MainActivity", "addInThread_2_#user=" + user);
mUserDao.insert(user);
}
int taskNum = task.incrementAndGet();
Log.d("MainActivity", "addInThread#2#taskNum=" + taskNum);
}).start();
new Thread(() -> {
for (;;) {
List userList = mUserDao.queryBuilder()
.orderDesc(UserDao.Properties.Id)
.list();
if (userList == null) {
continue;
}
int taskNum = task.get();
Log.d("MainActivity", "updateInThread#task=" + taskNum);
if (taskNum >= 2) {
break;
}
Log.d("MainActivity", "addInThread#2#taskNum=" + taskNum);
userList.stream()
.filter((User user) -> (user.getId() % 3) == 0)
.forEach(user -> {
user.setName("name_updateInThread_" + user.getId());
mUserDao.update(user);
Log.d("MainActivity", "updateInThread#user=" + user);
});
}
int taskNum = task.incrementAndGet();
Log.d("MainActivity", "updateInThread#taskNum=" + taskNum);
}).start();
new Thread(() -> {
for (;;) {
mUserDao.detachAll();
List userList = mUserDao.queryBuilder()
.orderDesc(UserDao.Properties.Id)
.list();
int taskNum = task.get();
Log.d("MainActivity", "queryInThread#task=" + task);
if (taskNum >= 3) {
break;
}
Log.d("MainActivity", "queryInThread#userList=" + userList);
}
}).start();
}
public void addInThread(View view) {
new Thread(() -> {
for (int i = 0; i < 10000; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
int j = num.incrementAndGet();
User user = new User("name" + "_" + j);
Log.d("MainActivity", "addInThread#user=" + user);
mUserDao.insert(user);
}
}).start();
}
public void update(View view) {
List userList = mUserDao.queryBuilder().list();
userList.stream()
.filter((User user) -> (user.getId() % 3) == 0)
.forEach(user -> {
user.setName("name_update_" + user.getId());
mUserDao.update(user);
Log.d("MainActivity", "update#user=" + user);
});
}
public void updateInThread(View view) {
new Thread(() -> {
for (int i = 0; i < 10000; i++) {
try {
Thread.sleep(360);
} catch (InterruptedException e) {
e.printStackTrace();
}
List userList = mUserDao.queryBuilder()
.orderDesc(UserDao.Properties.Id)
.list();
userList.stream()
.filter((User user) -> (user.getId() % 3) == 0)
.forEach(user -> {
user.setName("name_updateInThread_" + user.getId());
mUserDao.update(user);
Log.d("MainActivity", "updateInThread#user=" + user);
});
}
}).start();
}
public void query(View view) {
mUserDao.detachAll();
List userList = mUserDao.queryBuilder().list();
Log.d("MainActivity", "query#userList=" + userList);
}
public void queryInThread(View view) {
new Thread(() -> {
for (int i = 0; i < 10000; i++) {
mUserDao.detachAll();
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
List userList = mUserDao.queryBuilder()
.orderDesc(UserDao.Properties.Id)
.list();
Log.d("MainActivity", "queryInThread#userList=" + userList);
}
}).start();
}
public void forCurrentThread(View view) {
new Thread(() -> {
Query query = mUserDao.queryBuilder()
.where(UserDao.Properties.Name.like("update"))
.orderDesc(UserDao.Properties.Id)
.build()
.forCurrentThread();
List userList = query.list();
Log.d("MainActivity", "forCurrentThread#userList=" + userList);
}).start();
}
/**
* 时间倒计时
*/
class AddCountDownTimer extends CountDownTimer {
public AddCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long l) {
Log.d("MainActivity", "AddCountDownTimer#onTick#l=" + l);
int j = num.incrementAndGet();
User user = new User("name" + "_" + j);
Log.d("MainActivity", "AddCountDownTimer#user=" + user);
mUserDao.insert(user);
}
@Override
public void onFinish() {
Log.d("MainActivity", "AddCountDownTimer#onFinish");
}
}
}
实体类:
package com.windfallsheng.mygreendao;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;
@Entity
public class User {
@Id
private Long id;
@Property(nameInDb = "NAME")
private String name;
@Generated(hash = 873297011)
public User(Long id, String name) {
this.id = id;
this.name = name;
}
@Generated(hash = 586692638)
public User() {
}
public User(String name) {
this.name = name;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
xml布局文件:
暂时未发现异常问题,待继续验证………
由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!
Hello , World !
感谢所有!