同步线程IllegalStateException异常 [英] Synchronized Thread IllegalStateException

查看:178
本文介绍了同步线程IllegalStateException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查,没有的主题似乎真的帮助,在这里我的问题是我启动一个线程在PopupScreen,等待它完成,然后用适当的通知关闭PopupScreen,这是我的code

I've searched and none of the topics really seems to help, my problem here is I start a thread on a PopupScreen, wait for it to finish and then close the PopupScreen with the appropriate notification, Here's my code

LoginThread lt  = new LoginThread(str1, str2);
lt.start();

synchronized(lt){
    try{
        lt.wait();
    }catch(InterruptedException ie){
        ie.printStackTrace();
    }
    //Everything works fine until here i add
    close();
   //Or
   this.close();

}

private class LoginThread extends Thread{
    public LoginThread(String str1, String str2){

    }

    public void run(){
        synchronized(this){
            notify();
        }
    }
}

任何建议,我怎么可以关闭此popuscreen?

Any suggestion how I can close this popuscreen?

推荐答案

急救员应该知道,这是一个黑莓的Java问题,还有关于该处理的BlackBerry Java的影响是也许不是在提供的code明显

Responders should be aware that this is a BlackBerry Java question, and there are BlackBerry Java implications regarding this processing that are perhaps not apparent in the code supplied.

从BlackBerry Java透视图中,有许多原因,显示PopupScreen,其中之一就是要锁定用户,而一些后台处理发生。然而,这种锁定是从来没有使用的线程的.wait实现。黑莓UI处理使用事件驱动模型,按照我的理解有点像秋千(我不是一个Swing专家)。这样的结果是,如果你尝试code等待和您试图阻止用户上移动,则必须等待事件线程,以阻止用户输入完全。如果你尝试这样做(除了在模拟器,它有时让你做它出于某种原因)的黑莓OS barfs。

From a BlackBerry Java perspective, there are many reasons for displaying a PopupScreen, one of which is to lock out the user while some background processing takes place. However this lockout is never achieved using thread.wait. The BlackBerry Ui processing uses an Event driven model, as I understand it a bit like Swing (I am not a Swing expert). The upshot of this is if you try to code a wait and you are attempting to stop the user moving on, then you must wait on the Event Thread, which blocks user input completely. The BlackBerry OS barfs if you try to do this (except on the Simulator, where it sometimes lets you do it for some reason).

有关更多相关信息,在这里看到:

For more on this, see here:

什么 - 是 - 在-事件线程

在这种情况下,用户希望关闭弹出屏幕一旦长期运行的处理已完成。我建议这样做,使用类似于Observer模式的模式,换句话说,有螺纹回调在完成后观察者(PopupScreen)。然后观察员(屏)可以关闭本身。

In this case, the user wishes to close the popup screen once the long running processing has completed. I recommend doing that using an pattern like the Observer pattern, in other words, have the Thread call back the Observer (the PopupScreen) on completion. The Observer (screen) can then close itself.

在此同时,屏幕显示(观察)可以忽略,通常会关闭它,就像ESC键事件。该屏幕将显示一些对用户有用的,如请等待。

In the mean time, the screen displayed (the Observer) can ignore events that would normally close it, like the 'Esc' key. This screen will display something useful to the user, like "Please wait".

一个忠告然而,回调将在后台线程的地方,但是如果你想要做的UI处理,收盘必须在事件线程。交换到一个事件线程上下文是使用code像下面这样典型的做法:

One word of caution however, the call back will take place on the background Thread, but if you want to do Ui processing, the close must be on the Event Thread. Swapping to an Event Thread context is typically done using code like the following:

UiApplication.getUiApplication().invokeLater(new Runnable() {
  public void run() {
    // put code in here
  }
});

这一切说,我再次建议楼主搜索,因为我觉得有很多的样本这样的周围。在没有我所看到的样品中,有没有人使用的等待,所以我不太明白为什么OP试图首先使用等。如果有一个样品中被看见,请点我吧(在评论),这样我可以尝试修复它。

This all said, I suggest the Original Poster search again, since I think there are many samples like this around. In none of the samples that I have seen, has anyone used a wait, so I don't quite see why the OP attempted to use a wait in the first place. If this has been seen in a sample, please point me at it (in a comment) so that I can try to fix it.

这篇关于同步线程IllegalStateException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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