线程扩展,访问方法与提交的值

发布于 2025-02-09 11:18:58 字数 696 浏览 1 评论 0原文

因此,我正在尝试编写一个简单的类,该类正在扩展线程类。 我将非常感激

在写它的同时,我发现了一个有趣而令人困惑的伤亡,如果有人解释了我为什么确实有效的话,

@Override
public void run(){
    startTime = System.currentTimeMillis();
    running = true;
    while (isRunning()) {}
}

:但是,

@Override
public void run(){
    startTime = System.currentTimeMillis();
    running = true;
    while (running) {}
}

使用覆盖的方法:

@Override
public void interrupt(){
    running = false;
    Long endTime = System.currentTimeMillis();
    int time = Math.round(endTime - startTime);
    out.println(time);
}

btw isrunning()只是一个简单的getter女巫返回 运行 的布尔值。

So I am trying to write a simple class that is extending Thread class. While writing it I found a funny and confusing casualty and I would be very grateful if someone explain me

why this code does work:

@Override
public void run(){
    startTime = System.currentTimeMillis();
    running = true;
    while (isRunning()) {}
}

but this code doesn't:

@Override
public void run(){
    startTime = System.currentTimeMillis();
    running = true;
    while (running) {}
}

with overridden interrupt method:

@Override
public void interrupt(){
    running = false;
    Long endTime = System.currentTimeMillis();
    int time = Math.round(endTime - startTime);
    out.println(time);
}

BTW isRunning() is just a simple getter witch returns boolean value of running.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2025-02-16 11:18:58

您在这里拥有的是 种族条件 。在Java的特殊情况下,我们可以从 JLS

两个访问(读取或写入)同一变量被认为是相互冲突,如果至少一个访问是写入。

您有一个变量,并且有两个线程同时访问它,其中其中一个线程正在写入其中。要保护此数据,您需要 synchronize 两次访问。通常,建议在类型对象的私有实例变量上同步,以便没有其他人对该变量进行引用。 (同步方法的默认行为是在this上同步,如果有参考此同步的其他人,它可能会引起问题同样),

private boolean running;
private Object lock;

public MyClass() {
  lock = new Object();
}

...

@Override
public void run() {
  startTime = System.currentTimeMillis();
  synchronized (lock) {
    running = true;
  }
  while (isRunning()) {}
}

public boolean isRunning() {
  synchronized (lock) {
    return running;
  }
}

@Override
public void interrupt() {
  synchronized (lock) {
    running = false;
  }
}

而且,正如评论中已经指出的那样,当您做这样的事情时,通常会皱眉thread。考虑在Runnable子类中制作Run(),然后只需使用thread构造函数即可。

What you have here is a race condition. In the particular case of Java, we can read a direct definition from the JLS

Two accesses to (reads of or writes to) the same variable are said to be conflicting if at least one of the accesses is a write.

You have a single variable and you have two threads accessing it at the same time, where one of those threads is writing to it. To protect this data, you need to synchronize both accesses. Generally, it's advised to synchronize on a private instance variable of type Object, so that no one else has a reference to that variable. (The default behavior of synchronized methods is to synchronize on this, which can cause problems if someone else who has a reference to this synchronizes on it as well)

private boolean running;
private Object lock;

public MyClass() {
  lock = new Object();
}

...

@Override
public void run() {
  startTime = System.currentTimeMillis();
  synchronized (lock) {
    running = true;
  }
  while (isRunning()) {}
}

public boolean isRunning() {
  synchronized (lock) {
    return running;
  }
}

@Override
public void interrupt() {
  synchronized (lock) {
    running = false;
  }
}

And, as has already been pointed out in the comments, it is generally frowned upon to subclass Thread when you're doing things like this. Consider making your run() in a Runnable subclass and simply use the Thread constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文