简体   繁体   English

不断检查布尔值

[英]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中) :

  1. You just consume all available power of one CPU for nothing. 您只需消耗一个CPU的所有可用功率。
  2. With 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: 一些一般性建议:

  • If you want to perform some action when you receive user's input, you could simply do that in your event handler without using any flag or variable. 如果要在接收用户输入时执行某些操作,可以在事件处理程序中执行此操作,而不使用任何标志或变量。 However, this is possible only if your action is very short, otherwise you'll block the event dispatching thread . 但是, 只有当您的操作非常短时才可以执行此操作,否则您将阻止事件调度线程
  • If the action you need to perform takes even just a little bit longer, you should start a new thread from your event handler, which will perform the action and when it finishes, updates the GUI. 如果您需要执行的操作甚至需要更长的时间,您应该从事件处理程序启动一个新线程,该线程将执行操作,并在完成时更新GUI。 The most direct way is to use SwingWorker . 最直接的方法是使用SwingWorker Better read the whole chapter Concurrency in Swing to see all the possibilities. 更好地阅读Swing中的整个章节Concurrency以查看所有可能性。
  • If you need to communicate between two independent threads in general, like if you need one thread to wait until some flag changes, use one of Java's concurrency mechanisms instead of busy waiting. 如果您需要在两个独立线程之间进行通信,例如,如果您需要一个线程等待某个标志更改,请使用Java的并发机制之一而不是忙等待。 The most direct way how to replace busy waiting is discussed in Guarded Blocks . Guarded Blocks中讨论了如何替换繁忙等待的最直接方法。

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.

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