[英]Constantly checking a boolean value
I have 2 classes. 我有2节课。 The first class turns a boolean value to true when you click on a specific location, the second class calls frame.removeAll()
and frame.add(new CanvasSelect())
If the boolean in the first class is true. 当您单击特定位置时,第一个类将布尔值转换为true,第二个类调用frame.removeAll()
和frame.add(new CanvasSelect())
如果第一个类中的布尔值为true。 It seems to only check the boolean value once. 它似乎只检查一次布尔值。
Is there any way to make the second class constantly check the boolean value in the first class? 有没有办法让第二个类不断检查第一个类中的布尔值?
As suggested in the comments, you really need to post (a stripped-down version of) the code if you really want to know what's going on. 正如评论中所建议的那样,如果你真的想知道发生了什么,你真的需要发布(一个简化版本的)代码。 I can think of three possible reasons this might be happening. 我可以想到这可能发生的三个可能原因。
Possibility 1: You need to mark your boolean value as volatile
. 可能性1:您需要将布尔值标记为volatile
。 If there are multiple threads in play here then one of the threads might be seeing a stale value if the field is not marked as a volatile field. 如果此处存在多个线程,则如果该字段未标记为易失性字段,则其中一个线程可能会看到过时值。 However, it sounds like you're doing GUI programming (probably with Swing), so all these events should be happening in the GUI event thread—therefore I doubt this is the problem. 但是,听起来你正在进行GUI编程(可能是使用Swing),因此所有这些事件都应该发生在GUI事件线程中 - 因此我怀疑这是问题所在。 It still might be worth a try though. 尽管如此,它仍然值得一试。
Possibility 2: You are setting the event handler based on the boolean value rather than checking the boolean value within the event handler. 可能性2:您正在基于布尔值设置事件处理程序,而不是检查事件处理程序中的布尔值。 In this case the value would only be read once (when the ActionEventHandler is set) rather than at the time of the click event. 在这种情况下,该值只能读取一次(设置ActionEventHandler时)而不是click事件时。
Possibility 3: When you clear the frame you're clearing the items with the event handlers too, and you're not resetting them properly. 可能性3:当您清除框架时,您也使用事件处理程序清除项目,并且您没有正确地重置它们。 I think this is the most likely problem. 我认为这是最可能的问题。
Using busy waiting (as suggested in the other answer) in any form is a really bad idea, it's a programming anti-pattern (it should be used only under very special circumstances, like low-level CPU programming, and certainly never in Java): 使用任何形式的忙等待(如在另一个答案中所建议的)是一个非常糟糕的想法,它是一个编程反模式 (它应该只在非常特殊的情况下使用,例如低级CPU编程,当然从不在Java中) :
sleep
ing it's better, but then your code will react to a change with a delay. 有了sleep
它会更好,但是你的代码会对延迟的变化作出反应。 And the shorter delay you make, the more CPU power you waste. 延迟时间越短,浪费的CPU功率越大。 Still, it's a bad programming pattern. 不过,这是一个糟糕的编程模式。 Without a code snippet or a more detailed explanatin it's not clear what exactly you want to achieve. 如果没有代码片段或更详细的说明,则不清楚您想要实现的目标。 Some general suggestions: 一些一般性建议:
you can use code snippets like give below and put them into a thread which always run parallel to your main code 您可以使用下面给出的代码片段并将它们放入一个始终与主代码并行运行的线程中
while(true)
{
if(_val) //_val is your boolean variable
{
// your logic
}
Thread.sleep(100);
}
Or an event can be raised on the click of that particular control which checks value of _val
and performs actions 或者,可以通过单击特定控件来引发事件,该控件检查_val
值并执行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.