Junit 测试用例中的 Java 睡眠线程

发布于 2025-01-14 22:40:38 字数 1361 浏览 2 评论 0原文

我正在尝试创建数据的同步版本并使用 junit 来测试我的方法。下面的代码是我到目前为止所得到的。如果我放在 main 方法上(计数器由每个线程一一增加),效果会很好,但测试过程将立即停止。这是在测试用例上使用 Thread.sleep() 时出现的问题吗?

    public void testGeneral() {
        class SynchronizedData {
            public AtomicBoolean lock = new AtomicBoolean(false);
            public int counter = 0;

            public void update() {
                if(lock.compareAndSet(false, true)) {
                    counter++;
                    System.out.println(counter);
                    lock.set(false);
                }
            }
        }

        SynchronizedData data = new SynchronizedData();

        class Handler implements Runnable {
            String name;

            public Handler(String name) {
                this.name = name;
            }

            @Override
            public void run() {
                for(;;) {
                    try {
                        Thread.sleep(new Random().nextInt(100));
                    } catch (InterruptedException e) {
                        System.out.println(e);
                    }
                    System.out.println(this.name);
                    data.update();
                }
            }
        }

        new Thread(new Handler("One")).start();
        new Thread(new Handler("Two")).start();
    }

I am trying to create a synchronized version of data and using junit to test my method. The code below is what I came so far. It works quite well if I put on the main method (the counter is increased one by one by each thread) but the test process will stop immediately. Is this the problem by using Thread.sleep() on a test case?

    public void testGeneral() {
        class SynchronizedData {
            public AtomicBoolean lock = new AtomicBoolean(false);
            public int counter = 0;

            public void update() {
                if(lock.compareAndSet(false, true)) {
                    counter++;
                    System.out.println(counter);
                    lock.set(false);
                }
            }
        }

        SynchronizedData data = new SynchronizedData();

        class Handler implements Runnable {
            String name;

            public Handler(String name) {
                this.name = name;
            }

            @Override
            public void run() {
                for(;;) {
                    try {
                        Thread.sleep(new Random().nextInt(100));
                    } catch (InterruptedException e) {
                        System.out.println(e);
                    }
                    System.out.println(this.name);
                    data.update();
                }
            }
        }

        new Thread(new Handler("One")).start();
        new Thread(new Handler("Two")).start();
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文