【问题标题】:how to notify a thread from another class如何通知另一个类的线程
【发布时间】:2013-10-04 02:12:52
【问题描述】:

我试图从另一个班级调用我的通知我的线程,但我认为我做错了。我已经阅读了许多尝试类似事情的人的例子,但我似乎无法将它应用到我的代码中。我认为我的问题是我的同步对象,但我不确定。我是线程新手,所以不要羞辱我。这是我的代码示例:

public class SendTextOffEdt implements Runnable {
               private static final long SLEEP_TIME = 3000;
               public static String TEXT = "Sent off of EDT\n";
               private TextBox myTextBox;

               //public boolean waitBoolean = false;

               public SendTextOffEdt(TextBox myTextBox) {
                  this.myTextBox = myTextBox;
               }
            @Override
               public void run() {
                   synchronized(this){
                       //while (true) {
                     try {
                       myTextBox.appendTextOffEdt(TEXT);
                         wait();

                     } catch (InterruptedException e) { //******** i changed this from another exception!! 
                     System.out.println("*----------thread interrupted!");
                     myTextBox.appendTextOffEdt(TEXT);
                     }
                  }
               }
}

public class Combat extends JPanel {


private SendTextOffEdt sendTextOffEdt = new SendTextOffEdt(textbox); //just added this      object    

public Combat(){
TestNotify();
}

public void TestNotify(){
        synchronized(sendTextOffEdt){ //added the sendTextOffEdt object here

            sendTextOffEdt.notifyAll();
            System.out.println("has been notified");
        }
    }
}

【问题讨论】:

    标签: java multithreading swing concurrency


    【解决方案1】:

    wait() 与 this.wait() 相同,notifyAll() 与 this.notifyAll() 相同。因此,您可以看到您正在对完全独立的对象调用 wait() 和 notifyAll()。您必须在调用 wait() 的同一对象上调用 notifyAll()。

    您的 Combat 类需要对您的 SendTextOffEdt 类的引用。然后 Combat 类可以调用 SendTextOffEdt 实例上的 notifyAll() 来唤醒它。

    【讨论】:

    • 我编辑了我的代码以执行您的建议。让我知道这是否有效。我将 sendtextoffedt 对象添加到战斗类并引用它。如果我告诉您我只是在通知线程时尝试打印一个字符串,这可能会有所帮助。这有帮助吗?
    • @CameronRoberson 您的 wait() 调用后没有任何代码。当 SendTextOffEdt 唤醒时,它什么也不做。
    • 我在我的程序中添加了代码(不是这里的那个),它仍然没有做任何事情。它可能与 eventdispatch 线程有关。 Combat 类是从一个动作事件中调用的,我相信这意味着它在 EDT 上是否正确?我是否需要有一个 try catch 语句来检查这个?让我知道。谢谢杰,
    • @CameronRoberson 问题可能出在很多方面。如果没有看到更多代码,我将无能为力。
    • 我用代码发布了另一个问题。你能看一下吗? stackoverflow.com/questions/19239312/… 谢谢,
    【解决方案2】:

    为了实现对线程的最大控制; EDT(对于Swing)vs后台线程(对于耗时的进程),你必须掌握SwingWorker类。根据我的经验,创建 Runnables 和使用 synchronized 不适用于典型的应用程序开发。

    阅读我在此线程中的答案以获取更多信息:Performing two actions in order

    您还可以在此处阅读更多有关 Oracle 文档的信息:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

    简要介绍一下它的含义:SwingWorker 有两种方法:doInBackground() 始终在 EDT 之外运行,done() 始终在 EDT 上运行。

    当我编码时:我总是在SwingWorker 的构造函数中初始化一个加载对话框,运行我的后台代码,并提示用户后台进程的结果(无论是成功消息,将数据加载到表等)。

    请注意:在您掌握 SwingWorker 线程之前,我不会专注于 publish()process() 方法。

    【讨论】:

      猜你喜欢
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多