Java 有效地监视int值

Java 有效地监视int值,java,swing,Java,Swing,我将代码更改为更详细的版本,以便您更好地了解我的问题 我需要“监视”一个整数值,并在它发生变化时立即作出响应。到目前为止,我发现最好的方法是在无限循环中使用线程 下面是我项目的一个大大简化的部分。总而言之,单击my Bubble类中的按钮即可将notificationValue设置为1。我需要小程序能够监视此notificationValue,并在其更改时作出响应 这是我的小程序: public class MyApplet extends JApplet { Bubble myBubb

我将代码更改为更详细的版本,以便您更好地了解我的问题

我需要“监视”一个整数值,并在它发生变化时立即作出响应。到目前为止,我发现最好的方法是在无限循环中使用线程

下面是我项目的一个大大简化的部分。总而言之,单击my Bubble类中的按钮即可将notificationValue设置为1。我需要小程序能够监视此notificationValue,并在其更改时作出响应

这是我的小程序:

public class MyApplet extends JApplet
{
    Bubble myBubble = new Bubble();
    public void run()
    {
        new Thread(
        new Runnable() {
            public void run() {
                while(true) {
                    if(myBubble.getNotificationValue() == 1) {
                        /* here I would respond to when the
                        notification is of type 1 */
                        myBubble.resetNotificationValue;
                    }
                    else if(myBubble.getNotificationValue() == 2) {
                        /* here I would respond to when the
                        notification is of type 2 */
                        myBubble.resetNotificationValue;
                    }
                    else if(myBubble.getNotificationValue() != 2) {
                        /* if it is any other number other
                        than 0 */
                        myBubble.resetNotificationValue;
                    }

                    // don't do anything if it is 0
                }
            }
        }).start();
    }
}
这是我的班级:

public class Bubble extends JPanel
{
    public JButton bubbleButton;

    public int notificationValue = 0;

    public int getNotificationValue()
    {
        return notificationValue;
    }
    public void resetNotificationValue()
    {
        notificationValue = 0;
    }

    protected void bubbleButtonClicked(int buttonIndex)
    {
        notificationValue = buttonIndex;
    }

    public Bubble()
    {
        bubbleButton = new JButton();
        bubbleButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event)
            {
                bubbleButtonClicked(1);
            }
        });
    }
}

但是很明显,这使得CPU保持在100%的状态,根本没有效率。有什么更好的方法可以做到这一点?(假设我无法更改任何负责更改整数的方法。)

尝试添加
线程。sleep(1)以节省CPU。

如果该int恰好是JavaBean的属性,则可以使用

然而,我怀疑,如果您需要监视某个整数的值更改,那么您就会遇到设计问题。最好确保integer只能通过某些方法更改,并确保该方法基于新旧值处理所需的逻辑

当它发生变化时立即响应

这究竟需要多大的“即时性”?在while循环中添加
Thread.sleep(10)
,可能会将CPU负载降至接近零

有什么更好的方法可以做到这一点?(假设我无法更改任何负责更改整数的方法。)


更好的方法是不直接公开字段。封装好处的一个很好的例子——使用setter方法可以使实现observer模式变得很简单。

您可以不时检查变量的值以节省CPU。或者使用模式

您是否可以将整数封装在另一个类中,使用setter和getter包装并添加通知(通过观察者)?

您可以使用wait/notify。你可以使用执行器服务。很大程度上取决于您是否可以更改设置整数的代码。

假设您无法更改实际设置整数的代码,那么您就无能为力了。也就是说,如果在每次传递结束时调用Thread.yield(),线程对其他应用程序性能的影响将是最小的。

通常您会使用带有脉冲的锁来通知更改,以便等待的线程将唤醒,但这需要锁定一个对象并能够修改setter。但是听起来好像你无法将功能注入setter?但是你解决了线程中的逻辑问题-不要忘记访问已实现Swing组件的任何属性必须发生在EDTI上。我已经尝试了该线程。sleep(10)仍然很高。在我的项目中,我需要监视的整数封装在JPanel中,我使用getter/setter方法对其进行修改。我就是不能这样做,因为我的项目是这样写的。我会尽量修改我原来的帖子,这样你就能有更好的想法。我不能。如果你想看一看,我完全改变了我给出的代码示例,这样你可以更好地想象为什么我必须这样做。