我有一个带有主标签活动的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似乎导致应用程序崩溃.我收到以下错误消息: …
我有一个LockManager来管理几个线程的锁.有时线程是坏男孩,我必须杀死它们并要求LockManager释放所有锁.但是,由于我在java中使用ReentrantLock这是不可能的,我无法解锁另一个线程所拥有的锁.
我被迫使用Locks(不能使用信号量,这是功课的重点).是否有任何Java Lock实现允许我解锁其他线程拥有的锁?
到目前为止,我考虑的选项是:
您可能会觉得有用的额外资源:
我需要加载每秒(或两个)新图像.
以下代码不起作用:
System.Threading.Thread.Sleep(2000);
this.Image_LoadImage.Source = new BitmapImage(new Uri(@"D:\\connect2-1.gif"));
System.Threading.Thread.Sleep(2000);
this.Image_LoadImage.Source = new BitmapImage(new Uri(@"D:\\connect3-1.gif"));
Run Code Online (Sandbox Code Playgroud)
我看到的是该应用程序睡眠4秒,然后出现第二个图像.
我该怎么做?谢谢.