多线程访问sqlite数据库同步的问题 synchronized

public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
db.execSQL("DROP TABLE IF EXISTS person");
db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
Thread thread1 = new Thread1();
Thread thread2 =new Thread2();
Thread thread3 = new Thread3();
Thread thread4 = new Thread4();
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}

class Thread1 extends Thread implements Runnable {
@Override
public void run() {
synchronized (this) {
ContentValues cv1 = new ContentValues();
cv1.put("name", "李");
cv1.put("age", 17);
db.insert("person", null, cv1);
}
}

}
class Thread2 extends Thread implements Runnable{
@Override
public void run() {
synchronized (this){
ContentValues cv1 = new ContentValues();
cv1.put("name", "张");
cv1.put("age", 18);

        ContentValues cv2 = new ContentValues();
        cv2.put("name", "孙");
        cv2.put("age", 25);
        for (int i =0;i<10;i++){ db.insert("person", null, cv1); db.insert("person", null, cv2); }
    }
}

}

class Thread3 extends Thread implements Runnable{

@Override
public void run() {
    synchronized (this){
        ContentValues cv1 = new ContentValues();
        cv1.put("name", "王");
        cv1.put("age", 22);
        ContentValues cv2 = new ContentValues();
        cv2.put("name", "赵");
        cv2.put("age", 80);
        ContentValues cv3 = new ContentValues();
        cv3.put("name", "jesse");
        cv3.put("age", 99);
        for (int i =0;i<10;i++){
            db.insert("person", null, cv1);
            db.insert("person", null, cv2);
            db.insert("person", null, cv3);
        }
    }

}

}
class Thread4 extends Thread implements Runnable{

@Override
public void run() {
   Cursor c = db.rawQuery("SELECT * FROM person where age = 99", null);
    while (c.moveToNext()) {
        int _id = c.getInt(c.getColumnIndex("_id"));
        String name = c.getString(c.getColumnIndex("name"));
        int age = c.getInt(c.getColumnIndex("age"));
        Log.i("db", "_id=>" + _id + ", name=>" + name + ", age=>" + age);
    }
    c.close();
}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
    return true;
}

return super.onOptionsItemSelected(item);

}

}


这是我今天刚刚在网上学习写的代码,写的肯定是不对了 - -想问一下是不是整个程序只能出现一个synchronized进行所有线程的同步 还有我的代码要如何修改呢?求大神指点。。。。万分感谢!!!

synchronized中的代码,相当于又回到了单线程。你整个设计都有问题。不要为了多线程而多线程。