{"id":1511,"date":"2011-09-01T06:18:33","date_gmt":"2011-09-01T11:18:33","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=1511"},"modified":"2011-09-01T06:18:33","modified_gmt":"2011-09-01T11:18:33","slug":"constructing-a-thread-when-you-dont-know-the-class-ahead-of-time","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2011\/09\/01\/constructing-a-thread-when-you-dont-know-the-class-ahead-of-time\/","title":{"rendered":"Constructing a thread when you don&#8217;t know the class ahead of time"},"content":{"rendered":"<p>While building a generic load testing toolkit, I found it useful to be able to use a standard invocation framework for running a thread class that contains the actual application code to be stress tested.  However, I had some problems determining how to dynamically create a set of threads when I didn&#8217;t know the class ahead of time.<\/p>\n<p>What is below is a simple framework to get you started.  <\/p>\n<pre lang=\"java\" line=\"1\">\r\npublic class loadUnknownThreadedClass {\r\n\r\n  public static void main(String args[]) throws Exception{\r\n\r\n    Thread[] threads = new Thread[5];\r\n\r\n    Class tmp = Class.forName(args[0]);\r\n    for(int i = 0;i < 5;i++) {\r\n      threads[i] = new Thread((Runnable)tmp.newInstance());\r\n      threads[i].start();\r\n    }\r\n  }\r\n}\r\n\r\nclass classA implements Runnable {\r\n  Thread t;\r\n\r\n  classA() {\r\n    t = new Thread(this);\r\n  }\r\n  public void run () {\r\n    System.out.println(\"in classA\");\r\n  }\r\n}\r\n\r\nclass classB implements Runnable {\r\n  Thread t;\r\n\r\n  classB() {\r\n    t = new Thread(this);\r\n  }\r\n  public void run () {\r\n    System.out.println(\"in classB\");\r\n  }\r\n}\r\n<\/pre>\n<p>The really important piece is below:<\/p>\n<pre lang=\"java\" line=\"1\">\r\n     Class tmp = Class.forName(args[0]);\r\n     threads[i] = new Thread((Runnable)tmp.newInstance());\r\n<\/pre>\n<p>This creates a class on the fly for what you want to dynamically run in a thread.  It then casts a new instance of this to a Runnable object, which is required by the Thread class constructor.<\/p>\n<p>Sample output is shown below.<\/p>\n<pre lang=\"text\">\r\nemgrid01:oracle:emprod1:\/home\/oracle>java loadUnknownThreadedClass classA\r\nin classA\r\nin classA\r\nin classA\r\nin classA\r\nin classA\r\nemgrid01:oracle:emprod1:\/home\/oracle>java loadUnknownThreadedClass classB\r\nin classB\r\nin classB\r\nin classB\r\nin classB\r\nin classB\r\nemgrid01:oracle:emprod1:\/home\/oracle>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While building a generic load testing toolkit, I found it useful to be able to use a standard invocation framework for running a thread class that contains the actual application code to be stress tested. However, I had some problems&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2011\/09\/01\/constructing-a-thread-when-you-dont-know-the-class-ahead-of-time\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[24,25],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1511"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1511"}],"version-history":[{"count":15,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1511\/revisions"}],"predecessor-version":[{"id":1529,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1511\/revisions\/1529"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1511"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}