「已注销」 2016-06-02 02:20 采纳率: 0%
浏览 1101

写了一段 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);
}

}


  • 写回答

2条回答 默认 最新

  • g01235 2016-06-02 02:57
    关注

    创建一个线程,进行多次实例化

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序