Android多线程安全访问数据库

原地址 假设你已编写了自己的SQLiteOpenHelper1public class DatabaseHelper extends SQLiteOpenHelper { ... } 若想在独立的线程中写数据例如:12345678910111213// Thread 1 Context context = getApplicationContext(); DatabaseHelper helper = new DatabaseHelper(context); SQLiteDatabase database = helper.getWritableDatabase(); database.insert(…); database.close(); // Thread 2 Context context = getApplicationContext(); DatabaseHelper helper = new DatabaseHelper(context); SQLiteDatabase database = helper.getWritableDatabase(); database.insert(…); database.close(); 在logcat中会报如下错误1android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)     继续阅读
MXlwq's avatar
MXlwq 8月 12, 2016
 | 

Java多线程

20160505修改部分代码20160506添加进程同步 继承Thread类123456789101112131415161718192021class Thread1 extends Thread { public void run() { int ticket = 5; while (ticket > 0) System.out.println(Thread.currentThread().getName() + "剩余" + (ticket--)); }}public class Main { public static void main(String[] args) { Thread1 mTh1 = new Thread1(); Thread1 mTh2 = new Thread1(); mTh1.start(); mTh2.start(); }} 运行结果:Thread-1剩余5Thread-1剩余4Thread-1剩余3Thread-1剩余2Thread-0剩余5Thread-1剩余1Thread-0剩余4Thread-0剩余3Thread-0剩余2 Thread-0剩余1一个继承了Thread的类的实例对象,无论调用多少次start方法,结果都只有一个线程在运行     继续阅读
MXlwq's avatar
MXlwq 5月 01, 2016
 |