首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么即使我在线程中更新了filteredList,它也是空的?

为什么即使我在线程中更新了filteredList,它也是空的?
EN

Stack Overflow用户
提问于 2016-08-14 17:27:29
回答 1查看 51关注 0票数 0

我在android的后台线程中执行后台操作,如下所示:

代码语言:javascript
运行
复制
switch (position) {   

    final List<Profile> filteredList = new ArrayList<>(0);
    case BELATED:
            Runnable belatedRunnable = new Runnable() {
                @Override
                public void run() {
                    for (Profile profile : allBirthdayProfiles) {
                        String birthday = profile.getBirthday();

                        String[] values = birthday.split("-");
                        int day = Integer.parseInt(values[0]);
                        int month = Integer.parseInt(values[1]);

                        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                        Calendar calendar = Calendar.getInstance();
                        String currentDate = formatter.format(calendar.getTime());
                        String[] currentDateValues = currentDate.split("-");
                        int currentDay = Integer.parseInt(currentDateValues[0]);
                        int currentMonth = Integer.parseInt(currentDateValues[1]);

                        if (month < currentMonth)
                            filteredList.add(profile);
                        else if (month == currentMonth && day < currentDay)
                            filteredList.add(profile);
                            Log.d(TAG, filteredList + "   IN THREAD");
                    }
                }
            };
            Thread belatedThread = new Thread(belatedRunnable);
            belatedThread.setName("FilterBelatedThread");
            belatedThread.start();
            break;

        default:
            return allBirthdayProfiles;
    }
    Log.d(TAG, filteredList + "    END RESULT");
    return filteredList;
}

由于代码将配置文件附加到过滤列表(filteredList.add(profile),因此在线程运行时应修改List<Profile> filteredList

在此switch语句之外,我返回filteredList,由于正在运行if语句(满足条件),理论上它应该为非空,但是filteredList为空。

代码语言:javascript
运行
复制
D/BirthdayFilter: []    END RESULT
D/BirthdayFilter: [{"birthday":"02-08-2016","favouriteIds":["62InvCZG9jaEmUNiJsssTIjImqY2"],"firstName":"Edal","friendIds":["62InvCZG9jaEmUNiJsssTIjImqY2"],"lastName":"Grunt","userId":"62InvCZG9jaEmUNiJsssTIjImqY2"}]IN THREAD

为什么即使我在线程中更新了filteredList,它也是空的?

EN

回答 1

Stack Overflow用户

发布于 2016-08-14 17:47:23

这是您在这里遇到的一个典型的并发问题。Runnablerun方法中的代码与主代码并行执行,并且独立于主代码执行(这就是RunnableThread的全部用途)。因此,Java运行时不能保证执行的顺序!有时if语句会在return语句之前进行计算,但大多数情况下是相反的。

您可能想要做的是通知您的主线程run方法中所做的更改。根据您的设置,这可能与以下内容类似:

代码语言:javascript
运行
复制
Runnable belatedRunnable = new Runnable() {
    @Override
    public void run() {
        // ... normal code here
        updateList(); // notify main thread of changes
    }
}

然后将一个新方法添加到您的activity中,以便根据需要更新列表:

代码语言:javascript
运行
复制
private void updateList() {
    // update your list
}

或者,如果您将其用作独立的组件,则需要将侦听器传递给该方法,以便它可以将更改通知调用者。例如,创建一个新接口:

代码语言:javascript
运行
复制
public interface Updateable {
    void update();
}

然后在调用类中实现此接口,例如:

代码语言:javascript
运行
复制
public MyFragment extends Fragment implements Updateable {
    // ...
    @Override
    public void update() {
        // update your list here
    }
}

然后将此代码添加到您的方法中:

代码语言:javascript
运行
复制
public void filter(/* ... */ final Updateable listener) {
    // inside your run method:
    listener.update();
}

最后,在调用类中调用您的方法,如下所示:

代码语言:javascript
运行
复制
filter(/* ... */ this);

您可能希望考虑将列表传递给update接口和方法,这样就可以使用新数据直接更新列表。

或者,如果这是一个耗时的后台操作,您可能希望考虑使用具有易于使用的onPostExecute方法的AsyncTask

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38940862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档