gpt4 book ai didi

java - 如何解决 Android Java 中的并发问题?

转载 作者:行者123 更新时间:2023-11-29 22:47:12 24 4
gpt4 key购买 nike

当锁定设置为等待 5 秒时,getArt() 线程锁定,5 秒后线程恢复并返回响应。但是线程在设置锁后终止。

如何防止线程被终止并强制它在锁等待期间继续获取响应,并且在响应返回后,它应该 mLock.notifyAll();恢复一切,我的代码工作。

我之前在没有while循环的情况下尝试过,它确实在其他方法将值用作null后返回了响应。

请帮忙。

private void setArt(String imgArt){
this.imgArt = imgArt;
}

private void setDone(){
this.done = true;
}

private void getArt(){
mLock = new Object();

Thread thread = new Thread() {
@Override
public void run() {
try {
StringRequest stringRequest = new StringRequest(Request.Method.POST, String.format("%sFetchMP3Image.php", Station_Util.URL),
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.out.println("Response received.");
JSONObject Response = new JSONObject(response);
String Status = Response.getString("Response");
if (Status.equals("Success")) {
JSONObject MP3File = Response.getJSONObject("MP3");
String ImageURL = MP3File.getString("ImageURL");
setArt(ImageURL);
mLock.notifyAll();
setDone();
}
} catch (Exception e) {

}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("MP3Name", "687956770_1134920615_1196563259.mp3");
return params;
}

};

Volley.newRequestQueue(My_Downloads.this).add(stringRequest);
} catch (Exception e) {
}
}
};
thread.start();

try {
while(!this.done) {
synchronized (mLock) {
System.out.println("Thread Alive Before: " + thread.isAlive());
System.out.println("Thread Alive Before: " + thread.getState());
System.out.println("Thread Alive Before: " + thread.isInterrupted());
mLock.wait(5000);
System.out.println("Thread Alive After: " + thread.isAlive());
System.out.println("Thread Alive After: " + thread.getState());
System.out.println("Thread Alive After: " + thread.isInterrupted());
}
}
SetList();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

编辑 1:我自己解决了这个问题,使用辅助线程读取值,而不是像以前那样使用 UI 线程。感谢您的赞成票和任何试图提供帮助的人。

解决方法:

private void setArt(String imgArt){
this.imgArt = imgArt;
}

private void setDone(){
this.done = true;
}

private boolean getDone()
{
return this.done;
}

private void setListed(){
this.listed = true;
}

private boolean getListed(){
return this.listed;
}

private void getArt(){
mLock = new Object();

final Thread thread = new Thread() {
@Override
public void run() {
try {
synchronized (mLock) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, String.format("%sFetchMP3Image.php", Station_Util.URL),
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.out.println("Response received: " + response);
JSONObject Response = new JSONObject(response);
String Status = Response.getString("Response");
if (Status.equals("Success")) {
System.out.println("Status: " + Status);
JSONObject MP3File = Response.getJSONObject("MP3");
String ImageURL = MP3File.getString("ImageURL");
System.out.println("ImageURL: " + ImageURL);
setArt(ImageURL);
//mLock.notifyAll();
setDone();
System.out.println("Done in API thread: " + getDone());
}
} catch (Exception e) {

}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("MP3Name", "687956770_1134920615_1196563259.mp3");
return params;
}

};

Volley.newRequestQueue(My_Downloads.this).add(stringRequest);
}
} catch(Exception e){
}
}
};
thread.start();

Thread thread2 = new Thread() {
@Override
public void run() {
try {
while (!getDone()) {
synchronized (mLock) {
mLock.wait(250);
System.out.println("API Thread Alive: " + thread.isAlive());
System.out.println("Done in Reading Thread: " + getDone());

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
System.out.println("UI Thread Done: " + getDone());
if (getDone()){
setListed();
SetList();
}
}
});
}
}

if (getDone() && !getListed()) {
setListed();
SetList();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread2.start();
}

最佳答案

您不应在同步块(synchronized block)外使用 notifyAll 方法。否则,异常 java.lang.IllegalMonitorStateException将被抛出。

当您使用wait 方法时,锁变量将被释放,内部线程将能够使用此监视器。

关于java - 如何解决 Android Java 中的并发问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58241002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com