Multithreading 返回在线程中更改/初始化的值
在我的j2me JAVA应用程序中,有一个线程初始化一个对象,然后我必须在另一个类中返回该对象的值 在我的主类中,我调用该线程之后,我必须获得线程中更改的值,但问题是在线程完成之前,调用该函数并返回null,程序不会继续 我所做的是将返回值放置在线程中,直到布尔状态没有变为真,但由于while循环,它挂起在那里,不会返回 我正在发布下面的代码让我知道最好的解决方案,记住这段代码是在J2me(Java)中,它的功能甚至对于线程来说都是有限的,所以不要建议我使用像Latch或BackgroundWorker这样的方法,因为它在这里不起作用 这是hsa返回值的线程和其他函数Multithreading 返回在线程中更改/初始化的值,multithreading,java-me,jsr75,Multithreading,Java Me,Jsr75,在我的j2me JAVA应用程序中,有一个线程初始化一个对象,然后我必须在另一个类中返回该对象的值 在我的主类中,我调用该线程之后,我必须获得线程中更改的值,但问题是在线程完成之前,调用该函数并返回null,程序不会继续 我所做的是将返回值放置在线程中,直到布尔状态没有变为真,但由于while循环,它挂起在那里,不会返回 我正在发布下面的代码让我知道最好的解决方案,记住这段代码是在J2me(Java)中,它的功能甚至对于线程来说都是有限的,所以不要建议我使用像Latch或BackgroundWo
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;
}
所以*问题行在完成之前返回对象,因此为空 您的问题是代码没有同步 您需要使用方法
wait()
和notify()
,如下所示:
public synchronized void run() {
try {
contacts.removeAllElements();
//...
readComplete = true;
this.notify();
} catch (Exception e) {
}
}
//Return contacts loaded into vector list
public synchronized // <-- note sync'd here
ContactVector getLoadedContacts() {
while(!readComplete){
this.wait();
}
return contacts;
}
public synchronized void run(){
试一试{
contacts.removeAllElements();
//...
readComplete=true;
this.notify();
}捕获(例外e){
}
}
//返回加载到向量列表中的联系人
public synchronized/您的问题是代码没有同步
您需要使用方法wait()
和notify()
,如下所示:
public synchronized void run() {
try {
contacts.removeAllElements();
//...
readComplete = true;
this.notify();
} catch (Exception e) {
}
}
//Return contacts loaded into vector list
public synchronized // <-- note sync'd here
ContactVector getLoadedContacts() {
while(!readComplete){
this.wait();
}
return contacts;
}
public synchronized void run(){
试一试{
contacts.removeAllElements();
//...
readComplete=true;
this.notify();
}捕获(例外e){
}
}
//返回加载到向量列表中的联系人
public synchronized//readComplete
-是否声明为volatile
?因为如果不是,您可能会遇到麻烦readComplete
-是否声明为volatile
?因为如果没有,您可能会遇到麻烦谢谢您的回复和建议,但是返回对象的代码仍然挂起:(@Saqib test以找出挂起的位置:1)在run
中简化代码,以便1)try catch只向控制台打印一些内容(我假设您在emulator上测试);如果可以的话,那么2)do for loop只打印一些可以的东西,将其缩小到“折叠”代码,而sub loop只打印一些东西等等。添加更多日志来跟踪一些东西等等。它在emulator上运行良好,但在设备上显示了我以前遇到的问题,我发现这是因为移动设备或固件。考虑到你的回答符合我的要求。感谢您的回复和建议,但返回对象的代码仍然挂起:(@Saqib test以找出挂起的位置:1)在run
中简化代码,以便1)try catch只向控制台打印一些内容(我假设您在emulator上测试);如果可以的话,那么2)do for loop只打印一些可以的东西,将其缩小到“折叠”代码,而sub loop只打印一些东西等等。添加更多日志来跟踪一些东西等等。它在emulator上运行良好,但在设备上显示了我以前遇到的问题,我发现这是因为移动设备或固件。考虑到你的回答符合我的要求。谢谢