「已注销」 2016-05-30 03:10 采纳率: 0%
浏览 1559

android 多线程线程如何同步

定义了三个线程public Thread thread1,thread2,thread3;分别执行操作,请问如何让这三个线程同步,贴出我的代码
public class MainActivity extends AppCompatActivity {
public static SQLiteDatabase db;
public static Person person;
public Thread thread1,thread2,thread3;
@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)");

     person = new Person();
    person.name = "john";
    person.age = 30;;
    db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[]{person.name, person.age});


    person.name = "david";
    person.age = 33;

    ContentValues cv = new ContentValues();
    cv.put("name", person.name);
    cv.put("age", person.age);

    person.name = "dasdd";
    person.age = 33;

    ContentValues cv1 = new ContentValues();
    cv1.put("name", person.name);
    cv1.put("age", person.age);
    person.name = "dasdd";
    person.age = 88;
    ContentValues cv2 = new ContentValues();
    cv2.put("name", person.name);
    cv2.put("age", person.age);
    db.insert("person", null, cv);
    db.insert("person", null, cv1);
    db.insert("person", null, cv2);

    thread1 = new Thread(new Runnable() {
@Override
public void run() {
        person.name = "abc";
        person.age = 100;
        ContentValues cv = new ContentValues();
        cv.put("name", person.name);
        cv.put("age", person.age);
        person.name = "axt";
        person.age = 67;
        ContentValues cv1 = new ContentValues();
        cv1.put("name", person.name);
        cv1.put("age", person.age);
        int i =1;
        while (i<10){
            db.insert("person", null, cv);
            db.insert("person", null, cv1);
            i++;
        }
    }
});
    thread1.start();;
    thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            person.name = "张";
            person.age = 47;
            ContentValues cv1 = new ContentValues();
            cv1.put("name", person.name);
            cv1.put("age", person.age);
            person.name = "QQQ";
            person.age = 999;
            ContentValues cv = new ContentValues();
            cv.put("name", person.name);
            cv.put("age", person.age);

                db.insert("person", null, cv);
                db.insert("person", null, cv1);

        }
    });
    thread2.start();

    thread3 =new Thread(new Runnable() {
        @Override
        public void run() {
            Cursor  c= db.rawQuery("SELECT * FROM person WHERE age >= ?",new String[]{"30"});
            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();
            }
        }
    });
    thread3.start();

//

   // db.delete("person", "age < ?", new String[]{"35"});

   // db.close();

//  deleteDatabase("test.db");


}

public class Person{
String name;
Integer age;
}
public synchronized void same(){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@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方法要怎么用?

  • 写回答

1条回答 默认 最新

  • 鲨鱼不会飞 2016-05-30 06:45
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料