简体   繁体   English

线程无限循环java

[英]threading infinity loop java

Inside a method, I start a thread that waits for user input (swing pushbutton). 在方法内部,我启动了一个等待用户输入的线程(摆动按钮)。 Only after that input, the thread can be closed and the method returns a value. 只有在输入之后,线程才能关闭,并且该方法返回一个值。 My problem is that the code waiting for the input is not run inside that thread, but elsewhere: 我的问题是等待输入的代码不在该线程中运行,而是在其他地方运行:

String returnString = "";
Thread waitThread = new Thread(
                () -> {
                    while (xy == null) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw THREAD_INTERRUPTED.toException();
                        }
                    }
                }, "waitThread"
            );

waitThread.start();

try {
    waitThread.join();
} catch (InterruptedException e) {
    throw THREAD_INTERRUPTED.toException();
}

// -> wait for user input -> xy != null -> waitThread ends -> main thread joins -> continue code:

returnString = xy;

return ReturnString;

Why is this necessary? 为什么这是必要的? Because the method has to return the value (xy) that is set elsewhere by clicking a pushbutton. 因为该方法必须返回通过单击按钮在其他位置设置的值(xy)。

The code above just ends up in an infinity loop, not allowing any interaction with the swing components. 上面的代码仅以无限循环结束,不允许与swing组件进行任何交互。

Not being a pro in swing, I suppose the main thread is meant to catch interaction events. 我想不是专业人士,我想主线程是用来捕获交互事件的。 Since it is stuck in the waitThread.join(); 由于它被卡在waitThread.join(); , thats not possible. ,那是不可能的。 Correct? 正确?

Is there a way to restructure this? 有没有办法重组它?

Why reinvent the wheel? 为什么要重新发明轮子? Plenty of ways to do this out-of-the-box: 开箱即用的多种方法:

public static void main(String[] args) {
    String message = JOptionPane.showInputDialog("What are you gonna tell me?");
    System.out.println(message);
}

JOptionPane的

I think you are going down the wrong route. 我认为您走错了路。

Clicking a button leads to an event, and then there should be an ActionListener reacting to that. 单击一个按钮会导致一个事件,然后应该有一个ActionListener对此做出反应。

And that listener could update some "statish" thingy, and your other thread is reading that information. 并且该侦听器可能会更新一些“状态”内容,而您的其他线程正在读取该信息。

To answer jannis' question: The method opens a popup window that holds lets say two buttons. 回答jannis的问题:该方法打开一个弹出窗口,其中包含两个按钮。 Each button sets a specific return value for the popup, which is then returned by the same method. 每个按钮都为弹出窗口设置一个特定的返回值,然后通过相同的方法将其返回。 So the method needs to open and close the popup. 因此,该方法需要打开和关闭弹出窗口。 I know this is stupid, but it has to be this way. 我知道这很愚蠢,但必须是这种方式。 The setup would work, if I could keep interaction with the frontend enabled while waiting somehow. 如果我可以在等待时保持与前端的交互,则设置将起作用。

Judging from this comment you seem to be trying to rediscover what is called a "modal dialog" and it's not stupid, at all. 从此评论来看,您似乎正在尝试重新发现所谓的“模态对话框”,而这根本不是愚蠢的。 Please see the official documentation about dialogs in Swing: How to Make Dialogs . 请参阅Swing: 如何制作对话框中有关对话框的官方文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM