我必须执行 2 种方法
await()
signal()
条件是在完成方法执行后,await()
只signal()
应调用该方法。
在我的情况下,应用程序正在强制关闭。
我在这里写了示例:
private final Object lock = new Object();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
signal();
try {
await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Method: after await");
}
public void signal() {
synchronized (lock) {
System.out.println("Method before ");
**lock.notify();**
System.out.println("Method await after signal ");
}
}
public void await() throws InterruptedException {
// synchronized (lock) {
Thread t = new Thread() {
public void run() {
System.out.println("Method await before wait ");
try {
**lock.wait();**
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Method in exception ");
}
System.out.println("Method await after wait ");
}
};
}