我想更新在另一个线程一个对象,然后访问它在当前线程:Java线程,这有什么错我的代码
public Object getValueAt(final int rowIndex, int columnIndex) {
data.getRealm().exec(new Runnable(){
@Override
public void run() {
System.out.println("Object at " + rowIndex + " in WritableList is " + data.get(rowIndex));
object = (DOModel) data.get(rowIndex);
System.out.println(object);
object.notify();
}
});
// suspend current thread
try {
synchronized (object){
object.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if(columnIndex == 0){
System.out.println(object);
return object.getId();
}
}
但是当我运行我的代码发生java.lang.IllegalMonitorStateException。
我更改了我的代码,请参阅代码中的注释: 编辑--------------------------------
public Object getValueAt(final int rowIndex, int columnIndex) {
data.getRealm().exec(new Runnable(){
@Override
public void run() {
System.out.println("Object at " + rowIndex + " in WritableList is " + data.get(rowIndex));
object = (DOModel) data.get(rowIndex);
synchronized(object){
object.notify();
}
}
});
try {
synchronized (object){
object.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// this line below is never executed.
System.out.println(object);
if(columnIndex == 0){
return object.getId();
}
}
可能重复的[爪哇Wait和通知:抛出:IllegalMonitorStateException](http://stackoverflow.com/questions/7126550/java-wait-and-notify- illegalmonitorstateexception) – assylias 2012-03-28 15:00:20