I come from the world .NETand, unfortunately, look at the Java source with eyes .NET.
The following code from Android Apps (although not for Android):
private class Worker implements Runnable {
private final Object mLock = new Object();
private Looper mLooper;
Worker(String name) {
Thread t = new Thread(null, this, name);
t.start();
synchronized (mLock) {
while (mLooper == null) {
try {
mLock.wait();
} catch (InterruptedException ex) {
}
}
}
}
public Looper getLooper() {
return mLooper;
}
public void run() {
synchronized (mLock) {
Looper.prepare();
mLooper = Looper.myLooper();
mLock.notifyAll();
}
Looper.loop();
}
public void quit() {
mLooper.quit();
}
}
I donβt quite understand how it works synchronized. At first I thought that the synchronized locking object was mLock, but if after the t.start()constructor thread first enters the synchronization block, it blocks it at mLock.wait()and implicitly blocks the "t" thread, blocking it from entering the synchronized block.
This is obviously wrong, because my phone rings as expected :)
, " " ( = > ), ...
... , mLock.wait() mLock mLock .
, , .