服务中的竞争条件(Android)(线程过多)

时间:2011-09-13 07:57:26

标签: java android arraylist handler

我不知道为什么,但是这个函数在Timer下循环,用户指定他想在更新之间等待多长时间。不知何故,函数暂停(挂起)在部件上,其中" 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);
        }
    }
};

UpdateGUI类

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,我没有考虑当没有更新它时要做什么,所以代码正在执行,但现在我添加了三秒也有延迟,一切正常。我知道这不是应用程序的最佳设计,但我现在无法提出比这更有效的方法。 这是更新期间的典型竞争条件,当没有任何更新时,所以简单的睡眠将有助于

1 个答案:

答案 0 :(得分:0)

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

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

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

每种方法的同步都会影响性能。不过,这对你来说无关紧要。