我有一个带有主标签活动的Android应用,以及各个标签内的多个活动.在我的主要活动的onCreate()中,我有一个可以创建列表的runnable,在各个活动中,我使用了这个列表.
在单个活动的onCreate()中,我还有可在列表上运行的Runnables.但是,我需要这些Runnables仅在主标签活动的Runnable完成创建列表时运行,否则我将得到一个空列表.我正试图找到一种优雅的方式来做到这一点.现在,在我的主要活动的Runnable中,我正在设置一个全局布尔变量isDone,在我的个人活动的Runnable中,我正在等待isDone通过while循环设置.这可行,但可能不是这样做的最佳方式.
有什么想法吗?
谢谢.
编辑:我正在尝试以下代码,但我收到运行时错误:
在我的MainActivity的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();
Run Code Online (Sandbox Code Playgroud)
在我的OtherActivity的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();
Run Code Online (Sandbox Code Playgroud)
mainRunnable似乎完全运行,但是otherRunnable似乎导致应用程序崩溃.我收到以下错误消息:
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
Run Code Online (Sandbox Code Playgroud)
Tay*_*mon 19
您可以使用wait和notify方法.
为此,需要一些全局可访问的对象,此时此程序中的其他任何内容都不会使用其锁定.我假设列表创建Runnable本身可以扮演这个角色.
所以你可以在list-creation Runnable类中添加这样的东西:
private boolean listsDone = false;
boolean getListsDone() {
return listsDone;
}
Run Code Online (Sandbox Code Playgroud)
run()在完成创建列表之后立即对其方法进行类似的操作:
synchronized (this) {
listsDone = true;
notifyAll();
}
Run Code Online (Sandbox Code Playgroud)
对于其他人Runnable的run()方法,他们需要等待这样的事情:
synchronized (listCreatingRunnableObject) {
if (!listCreatingRunnableObject.getListsDone()) {
try {
listCreatingRunnableObject.wait();
} catch (InterruptedException e) {
// handle it somehow
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:为了澄清,这两个synchronized模块需要在同一个对象进行同步,你必须打电话wait()和notifyAll()该对象上.如果对象是Runnable,那么它可以隐含在第一个(如上面的代码中),但如果它是活动,则需要在两种情况下显式使用活动对象.
64B*_*Bob 13
你可以使用Queue这样的:
public class RunQueue implemements Runnable
{
private List<Runnable> list = new ArrayList<Runnable>();
public void queue(Runnable task)
{
list.add(task);
}
public void run()
{
while(list.size() > 0)
{
Runnable task = list.get(0);
list.remove(0);
task.run();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这允许您使用一个线程而不是多个线程.并且您可以维护所有现有的"Runnable"对象,同时清理它们用于等待和连接的任何代码.
| 归档时间: |
|
| 查看次数: |
52980 次 |
| 最近记录: |