如何在Android Java中解决这个并发问题?

如何在Android Java中解决这个并发问题?,java,android,multithreading,concurrency,android-volley,Java,Android,Multithreading,Concurrency,Android Volley,当锁设置为等待5秒时,getArt()线程将锁定,在5秒后,线程将恢复并返回响应。但在设置锁后,线程将终止 如何防止线程被终止,并在锁等待时强制它继续获取响应,在返回响应后,它应该mLock.notifyAll();恢复一切,我的代码就能正常工作 我以前在没有while循环的情况下尝试过它,并且在值被另一个方法用作null之后,它确实返回了响应 请帮忙 private void setArt(String imgArt){ this.imgArt = imgArt; } private

当锁设置为等待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();
    }
}
private void setArt(字符串imgArt){
this.imgArt=imgArt;
}
私有void setDone(){
this.done=true;
}
私有无效getArt(){
mLock=新对象();
线程线程=新线程(){
@凌驾
公开募捐{
试一试{
StringRequest StringRequest=新的StringRequest(Request.Method.POST,String.format(“%sFetchMP3Image.php”,Station_Util.URL),
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
System.out.println(“收到响应”);
JSONObject响应=新JSONObject(响应);
字符串状态=Response.getString(“Response”);
如果(状态等于(“成功”)){
JSONObject MP3File=Response.getJSONObject(“MP3”);
字符串ImageURL=MP3File.getString(“ImageURL”);
setArt(ImageURL);
mLock.notifyAll();
setDone();
}
}捕获(例外e){
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“MP3Name”、“687956770_1134920615_1196563259.mp3”);
返回参数;
}
};
newRequestQueue(My_Downloads.this).add(stringRequest);
}捕获(例外e){
}
}
};
thread.start();
试一试{
而(!this.done){
已同步(mLock){
System.out.println(“之前的线程活动:+Thread.isAlive());
System.out.println(“之前的线程活动:+Thread.getState());
System.out.println(“之前的线程活动:+Thread.isInterrupted());
mLock.wait(5000);
System.out.println(“线程在:+Thread.isAlive()之后处于活动状态”);
System.out.println(“线程在:+Thread.getState()之后处于活动状态”);
System.out.println(“线程在:+Thread.isInterrupted()之后处于活动状态”);
}
}
SetList();
}捕捉(中断异常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();
}
private void setArt(字符串imgArt){
this.imgArt=imgArt;
}
私有void setDone(){
this.done=true;
}
私有布尔getDone()
{
归还这个。完成;
}
私有void setListed(){
此参数为true;
}
私有布尔getList(){
返回此列表;
}
私有无效getArt(){
mLock=新对象();
最终螺纹=新螺纹(){
@凌驾
公开募捐{
试一试{
已同步(mLock){
StringRequest StringRequest=新的StringRequest(Request.Method.POST,String.format(“%sFetchMP3Image.php”,Station_Util.URL),
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
System.out.println(“收到的响应:+响应”);
JSONObject响应=新JSONObject(响应);
字符串状态=Response.getString(“Response”);
如果(状态等于(“成功”)){
System.out.println(“状态:+状态”);
JSONObject MP3File=Response.getJSONObject(“MP3”);
字符串ImageURL=MP3File.getString(“ImageURL”);
System.out.println(“ImageURL:+ImageURL”);
setArt(ImageURL);
//mLock.notifyAll();
setDone();
System.out.println(“在API线程中完成:+getDone());
}
}捕获(例外e){
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}) {
@