In my j2me JAVA app there is a Thread which initializes an object which I right after that has to get by returning it's value in other class.
In my main class where I call that Thread right after that I have to get the value that was changed in Thread, but issue comes that before completion of the thread that function is called and it returns null and program doesn't proceed further.
What I did was to put that returning value wait till a boolean status doesn't get true inside the thread but because of that while loop it hangs there and doesn't come back.
I am posting the code below let me know the best solution, remember this code is in J2me (Java) which has limited functionality even for threading so don't suggest me methods like Latch or BackgroundWorker because it doesn't work here.
Here is the Thread and other function which hsa to return the value
public synchronized void run() {
try {
contacts.removeAllElements();
pim = PIM.getInstance();
String lists[] = pim.listPIMLists(PIM.CONTACT_LIST);
for (int i = 0; i < lists.length; i++) {
//code for custom backup operation
if (customCode == 1 && i == 0) {
continue;
} else if (customCode == 0 && i > 0) {
continue;
}
clist = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, lists[i]);
Enumeration cenum = clist.items();
while (cenum.hasMoreElements()) {
Contact c = (Contact) cenum.nextElement();
ContactDTO contact = new ContactDTO();
parseContactInfo(c, contact);
contacts.addElement(contact);
}
clist.close();
}
readComplete = true;
} catch (Exception e) {
}
}
//Return contacts loaded into vector list
public ContactVector getLoadedContacts() {
while(!readComplete){
Thread.sleep(100);
}
return contacts;
}
This is main class from where I have to call this
public ContactVector getContactVector() {
DeviceContactRetriever dcr = new DeviceContactRetriever(this, language);
dcr.start();
ContactVector vector = dcr.getLoadedContacts(); //problem line*
return vector;
}
so *problem line returns the object before completion and hence it's null.