{"id":75357,"date":"2018-03-29T16:02:30","date_gmt":"2018-03-29T13:02:30","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=75357"},"modified":"2018-03-28T11:11:14","modified_gmt":"2018-03-28T08:11:14","slug":"how-i-test-my-java-classes-for-thread-safety","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html","title":{"rendered":"How I Test My Java Classes for Thread-Safety"},"content":{"rendered":"<p>I touched on this problem in <a href=\"https:\/\/www.youtube.com\/watch?v=rC17YwowURQ\">one of my recent webinars<\/a>, now it&#8217;s time to explain it in writing. Thread-safety is an important quality of classes in languages\/platforms like Java, where we frequently share objects between threads. The issues caused by lack of thread-safety are very difficult to debug, since they are sporadic and almost impossible to reproduce on purpose. How do you test your objects to make sure they are thread-safe? Here is how I&#8217;m doing it.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n<figure id=\"attachment_75376\" aria-describedby=\"caption-attachment-75376\" style=\"width: 620px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/scent-of-a-woman.jpg\"><img decoding=\"async\" class=\"wp-image-75376 size-large\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/scent-of-a-woman-1024x576.jpg\" alt=\"\" width=\"620\" height=\"349\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/scent-of-a-woman-1024x576.jpg 1024w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/scent-of-a-woman-300x169.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/scent-of-a-woman-768x432.jpg 768w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/scent-of-a-woman.jpg 1330w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/a><figcaption id=\"caption-attachment-75376\" class=\"wp-caption-text\">Scent of a Woman (1992) by Martin Brest<\/figcaption><\/figure><\/p>\n<p>Let us say there is a simple in-memory bookshelf:<\/p>\n<pre class=\"brush:java\">class Books {\r\n  final Map&lt;Integer, String&gt; map =\r\n    new ConcurrentHashMap&lt;&gt;();\r\n  int add(String title) {\r\n    final Integer next = this.map.size() + 1;\r\n    this.map.put(next, title);\r\n    return next;\r\n  }\r\n  String title(int id) {\r\n    return this.map.get(id);\r\n  }\r\n}<\/pre>\n<p>First, we put a book there and the bookshelf returns its ID. Then we can read the title of the book by its ID:<\/p>\n<pre class=\"brush:java\">Books books = new Books();\r\nString title = \"Elegant Objects\";\r\nint id = books.add(title);\r\nassert books.title(id).equals(title);<\/pre>\n<p>The class seems to be thread-safe, since we are using the thread-safe <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/ConcurrentHashMap.html\"><code>ConcurrentHashMap<\/code><\/a> instead of a more primitive and non-thread-safe <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/HashMap.html\"><code>HashMap<\/code><\/a>, right? Let&#8217;s try to test it:<\/p>\n<pre class=\"brush:java\">class BooksTest {\r\n  @Test\r\n  public void addsAndRetrieves() {\r\n    Books books = new Books();\r\n    String title = \"Elegant Objects\";\r\n    int id = books.add(title);\r\n    assert books.title(id).equals(title);\r\n  }\r\n}<\/pre>\n<p>The test passes, but it&#8217;s just a one-thread test. Let&#8217;s try to do the same manipulation from a few parallel threads (I&#8217;m using <a href=\"https:\/\/github.com\/hamcrest\/JavaHamcrest\">Hamcrest<\/a>):<\/p>\n<pre class=\"brush:java\">class BooksTest {\r\n  @Test\r\n  public void addsAndRetrieves() {\r\n    Books books = new Books();\r\n    int threads = 10;\r\n    ExecutorService service =\r\n      Executors.newFixedThreadPool(threads);\r\n    Collection&lt;Future&lt;Integer&gt;&gt; futures =\r\n      new LinkedList&lt;&gt;();\r\n    for (int t = 0; t &lt; threads; ++t) {\r\n      final String title = String.format(\"Book #%d\", t);\r\n      futures.add(service.submit(() -&gt; books.add(title)));\r\n    }\r\n    Set&lt;Integer&gt; ids = new HashSet&lt;&gt;();\r\n    for (Future&lt;Integer&gt; f : futures) {\r\n      ids.add(f.get());\r\n    }\r\n    assertThat(ids.size(), equalTo(threads));\r\n  }\r\n}<\/pre>\n<p>First, I create a pool of threads via <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/concurrent\/Executors.html\"><code>Executors<\/code><\/a>. Then I submit ten objects of type <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/concurrent\/Callable.html\"><code>Callable<\/code><\/a> via <code>submit()<\/code>. Each of them will add a new unique book to the bookshelf. All of them will be executed, in some unpredictable order, by some of those ten threads from the pool.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Then I fetch the results of their executors through the list of objects of type <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/concurrent\/Future.html\"><code>Future<\/code><\/a>. Finally, I calculate the amount of unique book IDs created. If the number is 10, there were no conflicts. I&#8217;m using the <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Set.html\"><code>Set<\/code><\/a> collection in order to make sure the list of IDs contains only unique elements.<\/p>\n<p>The test passes on my laptop. However, it&#8217;s not strong enough. The problem here is that it&#8217;s not really testing the <code>Books<\/code> from multiple parallel threads. The time that passes between our calls to <code>submit()<\/code> is large enough to finish the execution of <code>books.add()<\/code>. That&#8217;s why in reality only one thread will run at the same time. We can check that by modifying the code a bit:<\/p>\n<pre class=\"brush:java\">AtomicBoolean running = new AtomicBoolean();\r\nAtomicInteger overlaps = new AtomicInteger();\r\nCollection&lt;Future&lt;Integer&gt;&gt; futures = new LinkedList&lt;&gt;();\r\nfor (int t = 0; t &lt; threads; ++t) {\r\n  final String title = String.format(\"Book #%d\", t);\r\n  futures.add(\r\n    service.submit(\r\n      () -&gt; {\r\n        if (running.get()) {\r\n          overlaps.incrementAndGet();\r\n        }\r\n        running.set(true);\r\n        int id = books.add(title);\r\n        running.set(false);\r\n        return id;\r\n      }\r\n    )\r\n  );\r\n}\r\nassertThat(overlaps.get(), greaterThan(0));<\/pre>\n<p>With this code I&#8217;m trying to see how often threads overlap each other and do something in parallel. This never happens and <code>overlaps<\/code> is equal to zero. Thus our test is not really testing anything yet. It just adds ten books to the bookshelf one by one. If I increase the amount of threads to 1000, they start to overlap sometimes. But we want them to overlap even when there&#8217;s a small number of them. To solve that we need to use <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/concurrent\/CountDownLatch.html\"><code>CountDownLatch<\/code><\/a>:<\/p>\n<pre class=\"brush:java\">CountDownLatch latch = new CountDownLatch(1);\r\nAtomicBoolean running = new AtomicBoolean();\r\nAtomicInteger overlaps = new AtomicInteger();\r\nCollection&lt;Future&lt;Integer&gt;&gt; futures = new LinkedList&lt;&gt;();\r\nfor (int t = 0; t &lt; threads; ++t) {\r\n  final String title = String.format(\"Book #%d\", t);\r\n  futures.add(\r\n    service.submit(\r\n      () -&gt; {\r\n        latch.await();\r\n        if (running.get()) {\r\n          overlaps.incrementAndGet();\r\n        }\r\n        running.set(true);\r\n        int id = books.add(title);\r\n        running.set(false);\r\n        return id;\r\n      }\r\n    )\r\n  );\r\n}\r\nlatch.countDown();\r\nSet&lt;Integer&gt; ids = new HashSet&lt;&gt;();\r\nfor (Future&lt;Integer&gt; f : futures) {\r\n  ids.add(f.get());\r\n}\r\nassertThat(overlaps.get(), greaterThan(0));<\/pre>\n<p>Now each thread, before touching the books, waits for the permission given by <code>latch<\/code>. When we submit them all via <code>submit()<\/code> they stay on hold and wait. Then we release the latch with <code>countDown()<\/code> and they all start to go, simultaneously. Now, on my laptop, <code>overlaps<\/code> is equal to 3-5 even when <code>threads<\/code> is 10.<\/p>\n<p>And that last <code>assertThat()<\/code> crashes now! I&#8217;m not getting 10 book IDs, as I was before. It&#8217;s 7-9, but never 10. The class, apparently, is not thread-safe!<\/p>\n<p>But before we fix the class, let&#8217;s make our test simpler. Let&#8217;s use <a href=\"http:\/\/static.javadoc.io\/org.cactoos\/cactoos\/0.29\/org\/cactoos\/matchers\/RunsInThreads.html\"><code>RunInThreads<\/code><\/a> from <a href=\"http:\/\/www.cactoos.org\">Cactoos<\/a>, which does exactly the same as we&#8217;ve done above, but under the hood:<\/p>\n<pre class=\"brush:java\">class BooksTest {\r\n  @Test\r\n  public void addsAndRetrieves() {\r\n    Books books = new Books();\r\n    MatcherAssert.assertThat(\r\n      t -&gt; {\r\n        String title = String.format(\r\n          \"Book #%d\", t.getAndIncrement()\r\n        );\r\n        int id = books.add(title);\r\n        return books.title(id).equals(title);\r\n      },\r\n      new RunsInThreads&lt;&gt;(new AtomicInteger(), 10)\r\n    );\r\n  }\r\n}<\/pre>\n<p><a href=\"https:\/\/www.amazon.com\/Java-Concurrency-Practice-Brian-Goetz\/dp\/0321349601\/ref=as_li_ss_tl?s=books&amp;ie=UTF8&amp;qid=1473053257&amp;sr=1-1&amp;keywords=java+concurrency+in+practice&amp;linkCode=sl1&amp;tag=yegor256com-20&amp;linkId=5804a52aa81542bf2f80db3af432b864\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-75380\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/java-concurrency-in-practice.png\" alt=\"\" width=\"250\" height=\"331\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/java-concurrency-in-practice.png 250w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/java-concurrency-in-practice-227x300.png 227w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><\/a><\/figure>\n<p>The first argument of <code>assertThat()<\/code> is an instance of <code>Func<\/code> (a functional interface), accepting an <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/atomic\/AtomicInteger.html\"><code>AtomicInteger<\/code><\/a> (the first argument of <code>RunsInThreads<\/code>) and returning <code>Boolean<\/code>. This function will be executed on 10 parallel thread, using the same latch-based approach as demonstrated above.<\/p>\n<p>This <code>RunInThreads<\/code> seems to be compact and convenient, I&#8217;m using it in a few projects already.<\/p>\n<p>By the way, in order to make <code>Books<\/code> thread-safe we just need to add <code>synchronized<\/code> to its method <code>add()<\/code>. Or maybe you can suggest a better solution?<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Java Code Geeks with permission by Yegor Bugayenko, partner at our <a href=\"\/\/www.javacodegeeks.com\/join-us\/jcg\/\" target=\"_blank\" rel=\"noopener\">JCG program<\/a>. See the original article here: <a href=\"http:\/\/www.yegor256.com\/2018\/03\/27\/how-to-test-thread-safety.html\" target=\"_blank\" rel=\"noopener\">How I Test My Java Classes for Thread-Safety<\/a><\/p>\n<p>Opinions expressed by Java Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I touched on this problem in one of my recent webinars, now it&#8217;s time to explain it in writing. Thread-safety is an important quality of classes in languages\/platforms like Java, where we frequently share objects between threads. The issues caused by lack of thread-safety are very difficult to debug, since they are sporadic and almost &hellip;<\/p>\n","protected":false},"author":593,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-75357","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How I Test My Java Classes for Thread-Safety - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"I touched on this problem in one of my recent webinars, now it&#039;s time to explain it in writing. Thread-safety is an important quality of classes in\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How I Test My Java Classes for Thread-Safety - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"I touched on this problem in one of my recent webinars, now it&#039;s time to explain it in writing. Thread-safety is an important quality of classes in\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/yegor256\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-29T13:02:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yegor Bugayenko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/yegor256\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yegor Bugayenko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html\"},\"author\":{\"name\":\"Yegor Bugayenko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\"},\"headline\":\"How I Test My Java Classes for Thread-Safety\",\"datePublished\":\"2018-03-29T13:02:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html\"},\"wordCount\":661,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html\",\"name\":\"How I Test My Java Classes for Thread-Safety - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2018-03-29T13:02:30+00:00\",\"description\":\"I touched on this problem in one of my recent webinars, now it's time to explain it in writing. Thread-safety is an important quality of classes in\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/how-i-test-my-java-classes-for-thread-safety.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How I Test My Java Classes for Thread-Safety\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\",\"name\":\"Yegor Bugayenko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"caption\":\"Yegor Bugayenko\"},\"description\":\"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.\",\"sameAs\":[\"http:\\\/\\\/www.yegor256.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/yegor256\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/yegor256\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/yegor256\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yegor-bugayenko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How I Test My Java Classes for Thread-Safety - Java Code Geeks","description":"I touched on this problem in one of my recent webinars, now it's time to explain it in writing. Thread-safety is an important quality of classes in","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html","og_locale":"en_US","og_type":"article","og_title":"How I Test My Java Classes for Thread-Safety - Java Code Geeks","og_description":"I touched on this problem in one of my recent webinars, now it's time to explain it in writing. Thread-safety is an important quality of classes in","og_url":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/yegor256","article_published_time":"2018-03-29T13:02:30+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Yegor Bugayenko","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/yegor256","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yegor Bugayenko","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html"},"author":{"name":"Yegor Bugayenko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d"},"headline":"How I Test My Java Classes for Thread-Safety","datePublished":"2018-03-29T13:02:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html"},"wordCount":661,"commentCount":1,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html","url":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html","name":"How I Test My Java Classes for Thread-Safety - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2018-03-29T13:02:30+00:00","description":"I touched on this problem in one of my recent webinars, now it's time to explain it in writing. Thread-safety is an important quality of classes in","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/how-i-test-my-java-classes-for-thread-safety.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"How I Test My Java Classes for Thread-Safety"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d","name":"Yegor Bugayenko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","caption":"Yegor Bugayenko"},"description":"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.","sameAs":["http:\/\/www.yegor256.com\/","https:\/\/www.facebook.com\/yegor256","https:\/\/www.linkedin.com\/in\/yegor256","https:\/\/x.com\/https:\/\/twitter.com\/yegor256"],"url":"https:\/\/www.javacodegeeks.com\/author\/yegor-bugayenko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/75357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/593"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=75357"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/75357\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=75357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=75357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=75357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}