Thursday, January 23, 2014

A question on the thread, expert guidance


            
I wrote in the Activity inside
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 there is such
private ContentResolver cr;
private List 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 ();
}


But when debugging the following statement always found an error, the error content is object not locked by thread before wait (), how does that matter
synchronized (obj) {
try {
wait ();
} Catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
Reply:
Synchronization, notifications are still waiting for the same object.
For example,
/ * Thead one * /
synchronized (someObject) {
someObject.wait ();
}

/ * Different thread / object * /
synchronized (someObject) {
someObject.notify ();
}

No comments:

Post a Comment