【问题标题】:Modifying a protected field value is not seen in subclass instance在子类实例中看不到修改受保护的字段值
【发布时间】:2018-11-23 09:48:18
【问题描述】:

这是我遇到的一个奇怪的情况。我有一个定义受保护字段的抽象基类。还有一个修改字段的公共方法。然后我有一些使用该字段的基类的子类。

我注意到,当我调用超类方法来修改字段时,对字段值的更改似乎并没有“传递”到子类的实例。

需要提到的另一件事是抽象类(以及它的子类)实现了Runnable。我认为这不会影响我所看到的,但多线程不是我的强项。

抽象基类:

public abstract class AbstractWidget implements Runnable {
  // Other fields and methods omitted for brevity.
  protected boolean running;

  public void shutDown() {
    running = false;
  }
}

子类:

public class ConcreteWidget extends AbstractWidget {
  // Other fields and methods omitted for brevity.

  @Override
  public void run() {
    running = true;
    while (running) {
      // ...
    }
    logger.info("Shutting down");
  }
}

所以,当我最终调用shutDown() 方法时,在线程中运行的子类实例不会中断循环并返回。

我以前多次使用从“外部”修改布尔字段的技术来停止“永久”线程。我不明白这里发生了什么。

更新:

下面是被调用的代码示例。

ConcreteWidget widet = new ConcreteWidget(...);
thread = new Thread(widget);
thread.start();
logger.info("Started");

...

logger.debug("shutting down");
widget.shutDown();

try {
  logger.debug("doing join on thread");
  thread.join();
  logger.debug("returned from join");
} catch (InterruptedException e) {
  logger.error("Exception", e);
}

对join() 的调用永远不会返回。

更新:

根据要求,我已经包含了我希望是我目前拥有的代码的完整(足够)示例。注意:我已采纳建议并将受保护的boolean 更改为AtomicBoolean。

public abstract class AbstractWidget implements Runnable {
  protected final AtomicBoolean   running  = new AtomicBoolean(true);
  public void shutDown() {
    running.set(false);
  }
}


public class ConcreteWidget extends AbstractWidget {
  @Override
  public void run() {
    while (running.get()) {
      // ... do stuff (including calling process() below)
    }
  }
  private void process() {
    try {
      // ... do stuff
    } catch (IOException e) {
      logger.error("Exception", e);
      running.set(false);
      return;
    }
  }
}

在“主”逻辑中:

  private void startService() {
    widget = new ConcreteWidget(...);
    thread = new Thread(widget);
    thread.start();
    logger.info("Started");
  }

  public void close() {
    logger.debug("shutting down service");
    widget.shutDown();

    try {
      logger.debug("doing join on service thread");
      thread.join();
      logger.debug("returned from join");
    } catch (InterruptedException e) {
      logger.error("Exception", e);
    }
  }

顺便说一句,它仍然不起作用。

【问题讨论】:

  • 可以分享调用shutDown()的代码吗?也许问题就在那里。
  • 为了简洁起见,您是否无意中在上面 ConcreteWidget 中遗漏的代码中声明了 protected boolean running;?
  • 我也这么认为并检查过。不,我没有重新定义子类中的字段。
  • 这可能更多的是不使用volatile/synchronized的问题。
  • 将字段类型更改为 AtomicBoolean,使其成为 final,并更改其状态。这保证了其他线程可以看到任何更改。布尔值不会出现这种情况(除非您将其设为易失性,或者您使用相同的锁从同步块中读取和写入它)。 docs.oracle.com/javase/8/docs/api/java/util/concurrent/…

标签: java abstract protected


【解决方案1】:

你的问题其实很简单。当你调用widget.shutDown(); 时,线程还没有真正启动,所以当线程真正启动时,它会将running 设置回true,并且永远不会停止。不要使用running 来终止循环,而是使用单独的stopped 变量。

public abstract class AbstractWidget implements Runnable {
  // Other fields and methods omitted for brevity.
  private volatile boolean running = false;
  private valatile boolean stopped = false;

  public boolean isRunning() {
    return running;
  }

  public boolean hasStopped() {
    return stopped;
  }

  public void shutDown() {
    stopped = true;
  }
}

public class ConcreteWidget extends AbstractWidget {
  // Other fields and methods omitted for brevity.

  @Override
  public void run() {
    running = true;
    while (!stopped) {
      // ...
    }
    running = false;
    logger.info("Shutting down");
  }
}

使用此设置,您可能需要等待一段时间才能停止,否则循环将永远不会运行。

ConcreteWidget widet = new ConcreteWidget(...);
thread = new Thread(widget);
thread.start();
logger.info("Started");
...

try {
  Thread.sleep(500); // <--
} catch (Exception e) {}
logger.debug("shutting down");
widget.shutDown();

try {
  logger.debug("doing join on thread");
  thread.join();
  logger.debug("returned from join");
} catch (InterruptedException e) {
  logger.error("Exception", e);
}

【讨论】:

  • 我试过这个,但它仍然没有打破循环。
  • @JosephGagnon 那么这可能意味着您正在关闭一个与循环不同的小部件。发布一个完整的最小示例来重现问题。
  • @JosephGagnon 我已经改变了答案。
【解决方案2】:

你会在thread.start(); 之后立即运行widget.shutDown(); 吗?

也许widget.shutDown(); 已经在running = true; 代码在run() 方法之前运行

【讨论】:

  • 没有。小部件和线程或多或少是在启动时创建的,而 close() 方法会在很久以后调用。
【解决方案3】:

[捂脸]

事实证明,问题在于线程处理挂起并且永远无法检查running 字段的状态。一旦我纠正了这个问题,它就可以正常工作了。

我确实更改了我的逻辑以使用 AtomicBoolean 而不是 boolean,所以感谢您提供的有用建议。

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 2016-01-26
    • 2011-06-22
    • 1970-01-01
    • 2013-05-27
    • 2023-01-20
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多