关于线程的一个问题,请高手指点
我在Activity里面写
Object obj = new Object();
Result result = new Result();
ContactTread contactTread = new ContactTread(this,obj,result);
contactTread.start();
synchronized (obj) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ContactTread.java里面是这样的
private ContentResolver cr;
private List<User> userList;
private Object obj;
private Result result;
public ContactTread(Context context,Object obj,Result result)
{
this.cr = context.getContentResolver();
UserHelper userHelper = new UserHelper(context);
userList = userHelper.getUsers();
this.obj = obj;
this.result = result;
}
@Override
public void run()
{
result.Value = writePhone();
synchronized (obj) {
notify();
}
notify();
}
但调试的时候发现下面的语句老是报错,报错的内容是object not locked by thread before wait(),请问是怎么回事
synchronized (obj) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
------解决方案--------------------我先猜测下你想要实现的功能:
主线程运行到activity里,开启另一个线程A,主线程要先等待,线程A完成它的任务再唤醒主线程
是这样吗?
------解决方案--------------------object not locked by thread before wait()
wait的对象要先获取它的锁,你拿的锁和wait的不是同一个对象