{"id":3232,"date":"2019-03-17T10:19:40","date_gmt":"2019-03-17T10:19:40","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3232"},"modified":"2024-03-30T11:24:16","modified_gmt":"2024-03-30T11:24:16","slug":"thread-code-examples-basics","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/thread-code-examples-basics\/","title":{"rendered":"Thread Code Examples Basics"},"content":{"rendered":"<p><strong>Reading a File extending <em>Thread<\/em> API<\/strong><\/p>\n<ol>\n<li>ReadFile.java has a run() method which implements the reading the file code within the try with resources block<\/li>\n<li>In the main method start method is called over the ReadFile class instance<\/li>\n<li>In thread we have coded is asynchrobnous(order of execution cannot be guaranteed) which we can see from the output below<\/li>\n<\/ol>\n<p><strong>TestThread.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.test;\r\n\r\nimport com.mugil.runnables.ReadFile;\r\n\r\npublic class TestThread {\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tReadFile objReadFileThread1 = new ReadFile();\r\n\t\tReadFile objReadFileThread2 = new ReadFile();\r\n\t\tReadFile objReadFileThread3 = new ReadFile();\r\n\t\t\t\t\r\n\t\tobjReadFileThread1.start();\r\n\t\tobjReadFileThread2.start();\r\n\t\tobjReadFileThread3.start();\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>ReadFile.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.runnables;\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.File;\r\nimport java.io.FileReader;\r\nimport java.io.IOException;\r\n\r\npublic class ReadFile extends Thread {\r\n\r\n public void run() {\r\n\r\n  try (BufferedReader reader = new BufferedReader(new FileReader(new File(&quot;E:\\\\JavaProjects\\\\JavaThreads\\\\src\\\\Sample.txt&quot;)))) {\r\n   String line = null;\r\n\r\n   while ((line = reader.readLine()) != null) {\r\n    System.out.println(Thread.currentThread().getName() + &quot; reading line &quot; + line);\r\n   }\r\n\r\n  } catch (IOException e) {\r\n   \/\/ TODO Auto-generated catch block\r\n   e.printStackTrace();\r\n  }\r\n\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThread-2 reading line Line1\r\nThread-0 reading line Line1\r\nThread-0 reading line Line2\r\nThread-0 reading line Line3\r\nThread-1 reading line Line1\r\nThread-1 reading line Line2\r\nThread-2 reading line Line2\r\nThread-1 reading line Line3\r\nThread-1 reading line Line4\r\nThread-1 reading line Line5\r\nThread-0 reading line Line4\r\nThread-0 reading line Line5\r\nThread-2 reading line Line3\r\nThread-2 reading line Line4\r\nThread-2 reading line Line5\r\n<\/pre>\n<p><strong>Reading a File implementing <em>Runnable<\/em> API<\/strong><\/p>\n<ol>\n<li>Now in the below code the runnable API is implemented rather than extending like Thread<\/li>\n<li>The run() is called over instance of ReadFile rather than start() method<\/li>\n<li>Calling run() method will start the execution of thread in the present running thread rather than creating new Thread for execution which can been seen in output <em>main reading line  rather than Thread-N reading line<\/em><\/li>\n<\/ol>\n<p><strong>TestThread.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.test;\r\n\r\nimport com.mugil.runnables.ReadFile;\r\n\r\npublic class TestThread {\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tReadFile objReadFileThread1 = new ReadFile();\r\n\t\tReadFile objReadFileThread2 = new ReadFile();\r\n\t\tReadFile objReadFileThread3 = new ReadFile();\r\n\t\t\t\t\r\n\t\tobjReadFileThread1.run();\r\n\t\tobjReadFileThread2.run();\r\n\t\tobjReadFileThread3.run();\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>ReadFile.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ReadFile implements Runnable {\r\n\r\n public void run() {\r\n\r\n  try (BufferedReader reader = new BufferedReader(new FileReader(new File(&quot;E:\\\\JavaProjects\\\\JavaThreads\\\\src\\\\Sample.txt&quot;)))) {\r\n   String line = null;\r\n\r\n   while ((line = reader.readLine()) != null) {\r\n    System.out.println(Thread.currentThread().getName() + &quot; reading line &quot; + line);\r\n   }\r\n\r\n  } catch (IOException e) {\r\n   \/\/ TODO Auto-generated catch block\r\n   e.printStackTrace();\r\n  }\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nmain reading line Line1\r\nmain reading line Line2\r\nmain reading line Line3\r\nmain reading line Line4\r\nmain reading line Line5\r\nmain reading line Line1\r\nmain reading line Line2\r\nmain reading line Line3\r\nmain reading line Line4\r\nmain reading line Line5\r\nmain reading line Line1\r\nmain reading line Line2\r\nmain reading line Line3\r\nmain reading line Line4\r\nmain reading line Line5\r\n<\/pre>\n<p>Methods to Manage thread are available on Thread class not in Runnable. So we can pass the runnable instance as parameter like one below<br \/>\n<strong>TestThread.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\nThread objThread = new Thread(runObj);\r\nobjThread.start();\r\n.\r\n.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reading a File extending Thread API ReadFile.java has a run() method which implements the reading the file code within the try with resources block In the main method start method is called over the ReadFile class instance In thread we have coded is asynchrobnous(order of execution cannot be guaranteed) which we can see from the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/thread-code-examples-basics\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364,141],"tags":[],"class_list":["post-3232","post","type-post","status-publish","format-standard","hentry","category-examples","category-threads"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3232"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3232\/revisions"}],"predecessor-version":[{"id":3238,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3232\/revisions\/3238"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}