java - 服务中的竞争条件(Android)(线程太多)

标签 java android arraylist handler

我不知道为什么,但这个函数在计时器下循环,用户指定他想要在更新之间等待多长时间。不知何故,该函数在“System.out.println(“检查以添加新注释”);”的部分暂停(挂起)。有趣的是,这段代码在整个应用程序卡住之前运行了几次,因此没有任何错误

public ArrayList<String> newComments = new ArrayList<String>();
private ArrayList<String> downloadedComments = new ArrayList<String>();
<小时/>
 onStart(){
    if (receivedComments != null) {fthread
                for (int i = 0; i < receivedComments.size(); i++) {
                    if (newComments.contains(receivedComments.get(i))) {
                        System.out.println("Contains.");
                        newComments.remove(receivedComments.get(i));
                    }
                }
            }
    }
<小时/>
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

        System.out.println("got msg");
        switch (msg.what) {
        case 1:
            int add = 0;
            System.out.println("start filling the comments");
            for (int i = 1; i < comments.size(); i++) {
                newComment = comments.get(i).text();
                // System.out.println(newComment);
                System.out.println("checking to add a new comment");
                if (!downloadedComments.contains(newComment)) {
                    newComments.add(newComment);
                    System.out.println("additing");
                    downloadedComments.add(newComment);
                    System.out.println("added");
                    add++;
                    // System.out.println("New comments");
                    // System.out.println(newComments);
                }
                // downloadedComments.add(newComment);
            }

            // System.out.println(add);

            break;

        case 2:
            System.out.println("time refresh");
            timeUpdate.scheduleAtFixedRate(new UpdateGui(context,
                    appWidgetManager), 1, 3000);
        }
    }
};
<小时/>

更新GUI类

private class UpdateGui extends TimerTask {
        Context context;

        public UpdateGui(Context context, AppWidgetManager appWidgetManager) {
            this.context = context;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                for (int i = 0; i < newComments.size(); i++) {
                    try {
                        System.out.println("sleeping");
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (newComments.size() > 0) {
                        notificationView.setViewVisibility(
                                R.id.widget_notificationtext,
                                LinearLayout.VISIBLE);
                        notificationView.setTextViewText(
                                R.id.widget_notificationtext,
                                String.valueOf(newComments.size()));
                    } else {
                        notificationView.setViewVisibility(
                                R.id.widget_notificationtext,
                                LinearLayout.INVISIBLE);
                    }
                    if (newComments.size() != 0) {
                        remoteViews.setTextViewText(R.id.widget_text,
                                newComments.get(i));
                    } else {
                        remoteViews.setTextViewText(R.id.widget_text,
                                "No unread comments");
                    }
                    appWidgetManager.updateAppWidget(thisWidget, remoteViews);
                    appWidgetManager.updateAppWidget(thisWidget,
                            notificationView);
                }
try {
                        System.out.println("sleeping");
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
        }

    }

由于我每次都等待 3 秒从线程更新我的 GUI,所以我没有考虑当没有什么可以更新它时该怎么办,所以代码立即执行,但现在我添加了三个那里也有几秒钟的延迟,一切都正常。我知道这不是应用程序的最佳设计,但目前我无法想出比这更有效的设计。 这是更新期间没有什么可更新的典型竞争条件,因此简单的 sleep 会有所帮助

最佳答案

我敢打赌这基本上是一个竞争条件问题。您正在使用一堆不同的线程(UI、计时器、处理程序,以及可能更多)。 ArrayList 不是同步或线程安全类,这意味着 downloadedComments 完全有可能在 .contains() 运行的同时进行更新,从而导致内部循环基本上永远运行。如果您不将数据传递到各个目标(从而确保两个地方没有任何内容被修改),则必须将访问包装在同步 block 中以防止并发访问。

请注意,您可以获得列表的同步版本:

List list = Collections.synchronizedList(new ArrayList(...));

每种方法的同步都会影响性能。不过,这对你来说可能并不重要。

关于java - 服务中的竞争条件(Android)(线程太多),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7398558/

相关文章:

java - Sonar - 存储副本 - 不应直接存储或返回可变成员

带有 Tomcat 8.0.5 的 JavaMelody 1.52.0 - 未找到监控

java - Android Stack NullPointer 在搜索小部件中选择建议

android - 在 Android 的 ListView 中为每个项目设置标签?

java - Android:使用 Perst Lite 保存数据,如何保存已删除的数据?

android - 如何通过网络将文档扫描到android

java - 如何检查ArrayList中与String对象关联的值?

arraylist - 如何制作和使用函数数组列表

java - 在 Dropwizard 中使用 Curl 命令实现 Restful Webservices Post 请求

java - 如何调整 BBQ api 生成的条形码大小?