यह आपको जो चाहिए वह करना चाहिए। यह इस विधि को प्रकृति में तुल्यकालिक बनाने के लिए ज्ञात ऑब्जेक्ट के साथ notify()
और wait()
का उपयोग करता है। run()
के अंदर कुछ भी यूआई थ्रेड पर चलाएगा और एक बार समाप्त होने पर doSomething()
पर नियंत्रण वापस कर देगा। यह निश्चित रूप से कॉलिंग थ्रेड को सोने के लिए रखेगा।
public void doSomething(MyObject thing) {
String sync = "";
class DoInBackground implements Runnable {
MyObject thing;
String sync;
public DoInBackground(MyObject thing, String sync) {
this.thing = thing;
this.sync = sync;
}
@Override
public void run() {
synchronized (sync) {
methodToDoSomething(thing); //does in background
sync.notify(); // alerts previous thread to wake
}
}
}
DoInBackground down = new DoInBackground(thing, sync);
synchronized (sync) {
try {
Activity activity = getFromSomewhere();
activity.runOnUiThread(down);
sync.wait(); //Blocks until task is completed
} catch (InterruptedException e) {
Log.e("PlaylistControl", "Error in up vote", e);
}
}
}
मुझे समझ में नहीं आता कि स्ट्रिंग पर कॉलिंग अधिसूचना() क्या करेगी? –