在运行另一个Runnable之前等待Runnable完成 [英] Waiting for a Runnable to complete before running another Runnable

查看:105
本文介绍了在运行另一个Runnable之前等待Runnable完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有主标签活动的Android应用,以及各个标签内的多个活动。在我的主要活动的onCreate()中,我有一个可以创建列表的runnable,在各个活动中,我使用了这个列表。

I have an Android app with a main tab activity, and several activities within the individual tabs. In my main activity's onCreate(), I have a runnable that creates a list, and in the individual activities, I make use of this list.

在个人活动的onCreate中(),我也有运行在列表上的Runnables。但是,我需要这些Runnables仅在主标签活动的Runnable完成创建列表时运行,否则我将得到一个空列表。我正试图找到一种优雅的方式来做到这一点。现在,在我的主要活动的Runnable中,我设置了一个全局布尔变量isDone,在我的个人活动的Runnable中,我正在等待isDone通过while循环设置。这可行,但可能不是最佳方式。

In the individual activities's onCreate(), I also have Runnables that operate on the list. However, I need these Runnables to only run when the main tab activity's Runnable completes creating the list, otherwise I'd get a null list. I'm trying to find an elegant way of doing this. Right now, in my main activity's Runnable, I'm setting a global boolean variable isDone, and in my individual activity's Runnable, I'm waiting for isDone to be set via a while loop. This works, but probably isn't the best way of doing so.

有什么想法吗?

谢谢。

编辑:
我正在尝试以下代码,但是我遇到了运行时错误:

I'm trying the following code out, but I'm getting runtime errors:

在我的MainActivity的Runnable中:

In my MainActivity's Runnable:

mainRunnable = new Runnable() {
  public void run() {
    try {
      generateList();
      synchronized(this) {
      listDone = true;
      notifyAll();
    }
  } catch (Exception e) {
      Log.e("BACKGROUND_PROC", e.getMessage());
    }
  }
};
Thread thread = new Thread(null, mainRunnable, "Background");
thread.start();

在我的OtherActivity的Runnable中:

In my OtherActivity's Runnable:

otherRunnable = new Runnable() {
  public void run() {
    synchronized(MainActivity.mainRunnable) {
      if (!MainActivity.getListDone()) {
        try {
          wait();
        } catch (InterruptedException e) {
        }
      }
    }
  }
};
Thread thread = new Thread(null, otherRunnable, "Background");
thread.start();

mainRunnable似乎完全运行,但是otherRunnable似乎导致应用程序崩溃。我收到以下错误消息:

The mainRunnable seems to run completely, but the otherRunnable seems to cause the app to crash. I get the following error message:

01-10 15:41:25.543: E/WindowManager(7074): Activity com.myapp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40539850 that was originally added here
01-10 15:41:25.543: E/WindowManager(7074): android.view.WindowLeaked: Activity com.myapp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40539850 that was originally added here


推荐答案

您可以使用等待通知方法。

为此,需要一些全局可访问的对象,此时此程序中的其他任何东西都不会使用其锁。我假设列表创建 Runnable 本身可以扮演这个角色。

To do this, there needs to be some globally accessible object whose lock isn't used by anything else in the program at this point in time. I'm assuming that the list-creating Runnable itself can play this role.

所以你可以添加像这到列表创建 Runnable 类:

So you could add something like this to the list-creating Runnable class:

private boolean listsDone = false;

boolean getListsDone() {
    return listsDone;
}

这样的 run()方法,在创建列表后立即执行:

And something like this to its run() method, immediately after it's done creating the lists:

synchronized (this) {
    listsDone = true;
    notifyAll();
}

和其他类似的东西 Runnable s' run()方法,在他们需要等待的时间点:

And something like this to the other Runnables' run() methods, at the point where they need to wait:

synchronized (listCreatingRunnableObject) {
    if (!listCreatingRunnableObject.getListsDone()) {
        try {
            listCreatingRunnableObject.wait();
        } catch (InterruptedException e) {
            // handle it somehow
        }
    }
}

更新:为了澄清,需要在同一个对象上同步 synchronized 块,你必须在该对象上调用 wait() notifyAll()。如果对象是 Runnable ,那么它可以隐含在第一个(如上面的代码中),但如果它是活动,则需要显式使用活动两种情况都有对象。

Update: To clarify, both synchronized blocks need to be synchronized over the same object, and you have to call wait() and notifyAll() on that object. If the object is the Runnable, then it can be implicit for the first one (as in the above code), but if it's the activity, you need to explicitly use the activity object in both cases.

这篇关于在运行另一个Runnable之前等待Runnable完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆