从计时器设置本地字段

发布于 2024-12-06 16:47:20 字数 553 浏览 1 评论 0原文

我在单元测试中遇到以下问题。带测试的类:

class TestClass {
    boolean stopTest = false;

    @Test
    public void test() {
        // do something
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                stopTest = true;
            }
        }, 1200);
        while(!stopTest) {
        }
        timer.cancel();
        // do assert
    }
}

此测试仅在 while 运算符上带有断点的调试中正常工作,但是如果我不在调试中或没有断点的情况下运行测试,他将无限工作。我尝试将 stopTest 更改为带有布尔字段的类,还尝试通过此类中的 get 和 set 方法进行工作。我做错了什么?

I have the following problem in my unit test. The class with tests:

class TestClass {
    boolean stopTest = false;

    @Test
    public void test() {
        // do something
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                stopTest = true;
            }
        }, 1200);
        while(!stopTest) {
        }
        timer.cancel();
        // do assert
    }
}

This test works right only in debug with breakpoint on while operator, but if I run test not in debug or without breakpoint, he works infinitely. I tried to change stopTest to class with boolean field, also a tried to work through get and set methods in this class. What I do wrong?

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

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

发布评论

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

评论(3

自找没趣 2024-12-13 16:47:20

由于并发的原因需要使用volatile修饰符,否则stopTest无法可靠地更新和读取。有关详细信息,请参阅 JLS §8.3.1.4 -易失性字段

volatile boolean stopTest = false;

You need to use the volatile modifier due to concurrency, otherwise stopTest cannot be reliably updated and read. For more information, see JLS §8.3.1.4 - volatile Fields.

volatile boolean stopTest = false;
如何视而不见 2024-12-13 16:47:20

您应该将 stopTest 声明为 volatile

另请参阅:

  • 易失性atomicboolean
  • <一个href="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CC8QFjAA&url=http://download.oracle.com/javase/tutorial/essen tial/concurrency/syncmeth.html&ei=6yWCTvSaNdHKiALIlYH4DA&usg=AFQjCNFwf7xMWmQDc0iCEHn8dBGTTqgWFw&sig2=iv9scKcu5uJZaSIJWteCpA" rel="nofollow noreferrer">同步方法
  • 并发教程

You should declare stopTest to be volatile.

See also:

暗藏城府 2024-12-13 16:47:20

@JRL 和@mrkhrts 是对的。但我想提出其他建议。

你真的不需要这里的计时器。您只想在单独的线程中运行一些代码,并使主线程等待,直到该异步任务完成。使用简单的线程和 join() 方法来完成此操作:

@Test
public void test() {
    // do something
    Thread t = new Thread() {
        public void run() {
            try {Thread.sleep(1200)} catch(InterruptedException e) {}
            // do what you want here
        }
    }
    t.start();

    t.join(); // this line will be bloked until the async task is done.
}

@JRL and @mrkhrts are right. But I would like to suggest something else.

You really do not need timer here. You just want to run some code in separat thread and make your main thread to wait until this asynchronous task is done. Do it using simple thread and join() method:

@Test
public void test() {
    // do something
    Thread t = new Thread() {
        public void run() {
            try {Thread.sleep(1200)} catch(InterruptedException e) {}
            // do what you want here
        }
    }
    t.start();

    t.join(); // this line will be bloked until the async task is done.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文