定义了三个线程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方法要怎么用?