{"id":406,"date":"2009-02-21T11:00:52","date_gmt":"2009-02-21T11:00:52","guid":{"rendered":"http:\/\/thekua.com\/atwork\/?p=406"},"modified":"2009-02-21T11:07:52","modified_gmt":"2009-02-21T11:07:52","slug":"controlling-time-threaded-execution","status":"publish","type":"post","link":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/","title":{"rendered":"Controlling Time: Threaded Execution"},"content":{"rendered":"<p><strong>Update (Feb 21):<\/strong> Thanks to some feedback from Taras and Lowell, the ThreadedRunner is the same as the java.util.concurrent.Executor class! I&#8217;ve updated the post to reflect this below. Thanks for the suggestion!<\/p>\n<p>Unit testing threaded software can sometimes prove slightly cumbersome. Many of the examples on the net often show examples that tie a number of responsibilities together. It might be easy to see something like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">  public class SomeExample {    \r\n    public void doSomeWork() {\r\n      Runnable runnable = new Runnable() {\r\n        public void run() {\r\n          \/\/ do some long running work\r\n        }        \r\n      };\r\n      new Thread(runnable).start();\r\n    }\r\n  }<\/pre>\n<p>The thing about this class above is to realise what responsibilities it is mixing, including the new work they would like to (the item in Runnable), as well as how they would like to run it (the separate thread). It&#8217;s difficult to tease these two responsibilities apart the way it&#8217;s declared. The first thing I&#8217;d do is remove the dependency on the new thread. I&#8217;ll start by making use of the <code>java.util.concurrent.Executor<\/code> \t\t:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public interface Executor {\r\n  void execute(Runnable runnable);\r\n}\r\n  \r\npublic class SingleThreadedExecutor implements Executor {\r\n  public void execute(Runnable runnable) {\r\n    runnable.run();\r\n  }\r\n}\r\npublic class NewThreadedExecutor implements Executor {\r\n  public void execute(Runnable runnable) {\r\n    new Thread(runnable).start();    \t\r\n  }\r\n}<\/pre>\n<p>We&#8217;ll inject the SingleThreadedExecutor in our unit tests, so we only ever have one thread of execution, and we can rely on tests being consistently reproducible. We&#8217;ll inject the NewThreadedExecutor when we need to assemble our application for production. Here&#8217;s the step by step refactoring.<\/p>\n<p><strong>Wrap inside our new implementation<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public class SomeExample {    \r\n  public void doSomeWork() {\r\n    Runnable runnable = new Runnable() {\r\n      public void run() {\r\n        \/\/ do some long running work\r\n      }\r\n    };\r\n    new NewThreadedExecutor().start(runnable);\r\n  }\r\n}<\/pre>\n<p><strong>Inject instance via the constructor<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public class SomeExample {\r\n  private final NewThreadedExecutor executor;\r\n  public SomeExample(NewThreadedExecutor executor) {\r\n    this.executor = executor;\r\n  }  \r\n  \r\n  public void doSomeWork() {\r\n    Runnable runnable = new Runnable() {\r\n      public void run() {\r\n        \/\/ do some long running work\r\n      }\r\n    };\r\n    executor.start(runnable);\r\n  }\r\n}<\/pre>\n<p><strong>Replace specific with interface<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public class SomeExample {\r\n  private final Executor executor;\r\n  public SomeExample(Executor executor) {\r\n    this.executor = executor;\r\n  }  \r\n  \r\n  public void doSomeWork() {\r\n    Runnable runnable = new Runnable() {\r\n      public void run() {\r\n        \/\/ do some long running work\r\n      }\r\n    };\r\n    executor.start(runnable);\r\n  }\r\n}<\/pre>\n<p>In the tests, you can now inject an instance of SingleThreadedExecutor, whilst in production mode, you use the NewThreadedExecutor. <\/p>\n<p><strong>Disclaimers<\/strong><br \/>\nOf course, this still leaves your testing strategy incomplete where you do want to test your software with the number of threads that you do expect, particularly when you need to share state across different processes. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update (Feb 21): Thanks to some feedback from Taras and Lowell, the ThreadedRunner is the same as the java.util.concurrent.Executor class! I&#8217;ve updated the post to reflect this below. Thanks for the suggestion! Unit testing threaded software can sometimes prove slightly cumbersome. Many of the examples on the net often show examples that tie a number [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,35,12],"tags":[],"class_list":["post-406","post","type-post","status-publish","format-standard","hentry","category-development","category-java","category-testing","post-preview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Controlling Time: Threaded Execution - patkua@work<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Controlling Time: Threaded Execution - patkua@work\" \/>\n<meta property=\"og:description\" content=\"Update (Feb 21): Thanks to some feedback from Taras and Lowell, the ThreadedRunner is the same as the java.util.concurrent.Executor class! I&#8217;ve updated the post to reflect this below. Thanks for the suggestion! Unit testing threaded software can sometimes prove slightly cumbersome. Many of the examples on the net often show examples that tie a number [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/\" \/>\n<meta property=\"og:site_name\" content=\"patkua@work\" \/>\n<meta property=\"article:published_time\" content=\"2009-02-21T11:00:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2009-02-21T11:07:52+00:00\" \/>\n<meta name=\"author\" content=\"Patrick\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@patkua\" \/>\n<meta name=\"twitter:site\" content=\"@patkua\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Patrick\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/\"},\"author\":{\"name\":\"Patrick\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"headline\":\"Controlling Time: Threaded Execution\",\"datePublished\":\"2009-02-21T11:00:52+00:00\",\"dateModified\":\"2009-02-21T11:07:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/\"},\"wordCount\":428,\"commentCount\":4,\"articleSection\":[\"Development\",\"Java\",\"Testing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/\",\"name\":\"Controlling Time: Threaded Execution - patkua@work\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\"},\"datePublished\":\"2009-02-21T11:00:52+00:00\",\"dateModified\":\"2009-02-21T11:07:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2009\\\/02\\\/controlling-time-threaded-execution\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Controlling Time: Threaded Execution\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\",\"name\":\"patkua@work\",\"description\":\"The intersection of technology and leadership\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\",\"name\":\"Patrick\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"caption\":\"Patrick\"},\"sameAs\":[\"http:\\\/\\\/thekua.com\\\/atwork\"],\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/author\\\/patrick\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Controlling Time: Threaded Execution - patkua@work","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:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/","og_locale":"en_US","og_type":"article","og_title":"Controlling Time: Threaded Execution - patkua@work","og_description":"Update (Feb 21): Thanks to some feedback from Taras and Lowell, the ThreadedRunner is the same as the java.util.concurrent.Executor class! I&#8217;ve updated the post to reflect this below. Thanks for the suggestion! Unit testing threaded software can sometimes prove slightly cumbersome. Many of the examples on the net often show examples that tie a number [&hellip;]","og_url":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/","og_site_name":"patkua@work","article_published_time":"2009-02-21T11:00:52+00:00","article_modified_time":"2009-02-21T11:07:52+00:00","author":"Patrick","twitter_card":"summary_large_image","twitter_creator":"@patkua","twitter_site":"@patkua","twitter_misc":{"Written by":"Patrick","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/#article","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/"},"author":{"name":"Patrick","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"headline":"Controlling Time: Threaded Execution","datePublished":"2009-02-21T11:00:52+00:00","dateModified":"2009-02-21T11:07:52+00:00","mainEntityOfPage":{"@id":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/"},"wordCount":428,"commentCount":4,"articleSection":["Development","Java","Testing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/","url":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/","name":"Controlling Time: Threaded Execution - patkua@work","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/#website"},"datePublished":"2009-02-21T11:00:52+00:00","dateModified":"2009-02-21T11:07:52+00:00","author":{"@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"breadcrumb":{"@id":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thekua.com\/atwork\/2009\/02\/controlling-time-threaded-execution\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thekua.com\/atwork\/"},{"@type":"ListItem","position":2,"name":"Controlling Time: Threaded Execution"}]},{"@type":"WebSite","@id":"https:\/\/thekua.com\/atwork\/#website","url":"https:\/\/thekua.com\/atwork\/","name":"patkua@work","description":"The intersection of technology and leadership","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thekua.com\/atwork\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae","name":"Patrick","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","caption":"Patrick"},"sameAs":["http:\/\/thekua.com\/atwork"],"url":"https:\/\/thekua.com\/atwork\/author\/patrick\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":8,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":419,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/406\/revisions\/419"}],"wp:attachment":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}