资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何测试Java类的线程安全性

这篇文章主要介绍了如何测试Java类的线程安全性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

专注于为中小企业提供网站制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业虎丘免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

线程安全性是Java等语言/平台中类的一个重要标准,在Java中,我们经常在线程之间共享对象。由于缺乏线程安全性而导致的问题很难调试,因为它们是偶发的,而且几乎不可能有目的地重现。如何测试对象以确保它们是线程安全的?

假如有一个内存书架

package com.mzc.common.thread;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 

* 功能: 内存书架 *

* * @author Moore * @ClassName Books. * @Version V1.0. * @date 2019.12.10 14:00:13 */ public class Books { final Map map = new ConcurrentHashMap<>(); /** *

* 功能: 存书,并返回书的id *

* * @param title : * @return int * @author Moore * @date 2019.12.10 14:00:16 */ int add(String title) { final Integer next = this.map.size() + 1; this.map.put(next, title); return next; } /** *

* 功能: 根据书的id读取书名 *

* * @param id : * @return string * @author Moore * @date 2019.12.10 14:00:16 */ String title(int id) { return this.map.get(id); } }

首先,我们把一本书放进书架,书架会返回它的ID。然后,我们可以通过它的ID来读取书名,像这样:

Books books = new Books();
String title = "Elegant Objects";
int id = books.add(title);
assert books.title(id).equals(title);

这个类似乎是线程安全的,因为我们使用的是线程安全的ConcurrentHashMap,而不是更原始和非线程安全的HashMap,对吧?我们先来测试一下:

public class BooksTest {
 @Test
 public void addsAndRetrieves() {
 Books books = new Books();
 String title = "Elegant Objects";
 int id = books.add(title);
 assert books.title(id).equals(title);
 }
}

查看测试结果:

如何测试Java类的线程安全性

测试通过了,但这只是一个单线程测试。让我们尝试从几个并行线程中进行相同的操作(我使用的是Hamcrest):

/**
 * 

* 功能: 多线程测试 *

* * @throws ExecutionException the execution exception * @throws InterruptedException the interrupted exception * @author Moore * @date 2019.12.10 14:16:34 */ @Test public void addsAndRetrieves2() throws ExecutionException, InterruptedException { Books books = new Books(); int threads = 10; ExecutorService service = Executors.newFixedThreadPool(threads); Collection> futures = new ArrayList<>(threads); for (int t = 0; t < threads; ++t) { final String title = String.format("Book #%d", t); futures.add(service.submit(() -> books.add(title))); } Set ids = new HashSet<>(); for (Future f : futures) { ids.add(f.get()); } assertThat(ids.size(), equalTo(threads)); }

首先,我通过执行程序创建线程池。然后,我通过Submit()提交10个Callable类型的对象。他们每个都会在书架上添加一本唯一的新书。所有这些将由池中的10个线程中的某些线程以某种不可预测的顺序执行。

然后,我通过Future类型的对象列表获取其执行者的结果。最后,我计算创建的唯一图书ID的数量。如果数字为10,则没有冲突。我使用Set集合来确保ID列表仅包含唯一元素。

我们看一下这样改造后的运行结果:

如何测试Java类的线程安全性

测试也通过了,但是,它不够强壮。这里的问题是它并没有真正从多个并行线程测试这些书。在两次调用commit()之间经过的时间足够长,可以完成books.add()的执行。这就是为什么实际上只有一个线程可以同时运行的原因。

我们可以通过修改一些代码再来检查它:

@Test
 public void addsAndRetrieves3() {
  Books books = new Books();
  int threads = 10;
  ExecutorService service = Executors.newFixedThreadPool(threads);
  AtomicBoolean running = new AtomicBoolean();
  AtomicInteger overlaps = new AtomicInteger();
  Collection> futures = new ArrayList<>(threads);
  for (int t = 0; t < threads; ++t) {
   final String title = String.format("Book #%d", t);
   futures.add(
     service.submit(
       () -> {
        if (running.get()) {
         overlaps.incrementAndGet();
        }
        running.set(true);
        int id = books.add(title);
        running.set(false);
        return id;
       }
     )
   );
  }
  assertThat(overlaps.get(), greaterThan(0));
 }

看一下测试结果:

如何测试Java类的线程安全性

执行错误,说明插入的书和返回的id数量是不冲突的。

通过上面的代码,我试图了解线程之间的重叠频率以及并行执行的频率。但是基本上概率为0,所以这个测试还没有真正测到我想测的,还不是我们想要的,它只是把十本书一本一本地加到书架上。

再来:

如何测试Java类的线程安全性

可以看到,如果我把线程数增加到1000,它们会开始重叠或者并行运行。

但是我希望即使线程数只有10个的时候,也会出现重叠并行的情况。怎么办呢?为了解决这个问题,我使用CountDownLatch:

@Test
  public void addsAndRetrieves4() throws ExecutionException, InterruptedException {
    Books books = new Books();
    int threads = 10;
    ExecutorService service = Executors.newFixedThreadPool(threads);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicBoolean running = new AtomicBoolean();
    AtomicInteger overlaps = new AtomicInteger();
    Collection> futures = new ArrayList<>(threads);
    for (int t = 0; t < threads; ++t) {
      final String title = String.format("Book #%d", t);
      futures.add(
          service.submit(
              () -> {
                latch.await();
                if (running.get()) {
                  overlaps.incrementAndGet();
                }
                running.set(true);
                int id = books.add(title);
                running.set(false);
                return id;
              }
          )
      );
    }
    latch.countDown();
    Set ids = new HashSet<>();
    for (Future f : futures) {
      ids.add(f.get());
    }
    assertThat(overlaps.get(), greaterThan(0));
  }

现在,每个线程在接触书本之前都要等待锁权限。当我们通过Submit()提交所有内容时,它们将保留并等待。然后,我们用countDown()释放锁,它们才同时开始运行。

查看运行结果:

如何测试Java类的线程安全性

通过运行结果可以知道,现在线程数还是为10,但是线程的重叠数是大于0的,所以assertTrue执行通过,ids也不等于10了,也就是没有像以前那样得到10个图书ID。显然,Books类不是线程安全的!

在修复优化该类之前,教大家一个简化测试的方法,使用来自Cactoos的RunInThreads,它与我们上面所做的完全一样,但代码是这样的:

@Test
  public void addsAndRetrieves5() {
    Books books = new Books();
    MatcherAssert.assertThat(
        t -> {
          String title = String.format(
              "Book #%d", t.getAndIncrement()
          );
          int id = books.add(title);
          return books.title(id).equals(title);
        },
        new RunsInThreads<>(new AtomicInteger(), 10)
    );
  }

assertThat()的第一个参数是Func(一个函数接口)的实例,接受AtomicInteger(RunsThreads的第一个参数)并返回布尔值。此函数将在10个并行线程上执行,使用与上述相同的基于锁的方法。

这个RunInThreads看起来非常紧凑,用起来也很方便,推荐给大家,可以用起来的。只需要在你的项目中添加一个依赖:


      org.llorllale
      cactoos-matchers
      0.18
    

最后,为了使Books类成为线程安全的,我们只需要向其方法add()中同步添加就可以了。或者,聪明的码小伙伴们,你们有更好的方案吗?欢迎留言,大家一起讨论。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页名称:如何测试Java类的线程安全性
文章地址:http://cdkjz.cn/article/gheeod.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220