{"id":695,"date":"2013-03-25T09:27:59","date_gmt":"2013-03-25T09:27:59","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=487"},"modified":"2025-03-08T16:39:51","modified_gmt":"2025-03-08T16:39:51","slug":"threads-implementation-using-runnable","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/threads-implementation-using-runnable\/","title":{"rendered":"Basic Threads Implementation &#8211; Old Way"},"content":{"rendered":"<p><strong  class=\"ctaHeader2\">Simple Thread implementing Runnable<\/strong><br \/>\n<strong>Counter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Counter implements Runnable{\r\n    public void run(){\r\n        System.out.println(&quot;I am Counter Run Method&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Main.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Counter objCounter = new Counter();\r\n        Thread objThread = new Thread(objCounter);\r\n        objThread.start();\r\n    }\r\n}\r\n<\/pre>\n<pre>\r\nI am Counter Run Method\r\n<\/pre>\n<p><strong>Main.java(Using Lambda Expression)<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Thread objThread = new Thread(() -&gt; {\r\n            System.out.println(&quot;I am lambda Counter method&quot;);\r\n        });\r\n\r\n        objThread.start();\r\n    }\r\n}\r\n<\/pre>\n<pre>\r\nI am lambda Counter method\r\n<\/pre>\n<p><strong  class=\"ctaHeader3\">new vs runnable vs terminated<\/strong><\/p>\n<ol>\n<li>When you create thread using new Operator it would be in <strong>&#8220;new&#8221;<\/strong> state<\/li>\n<li>Once you call start method over thread it would Transend to <strong>&#8220;runnable&#8221;<\/strong> state<\/li>\n<li>When run method completed execution it would Transend to <strong>&#8220;terminated&#8221;<\/strong> state<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader3\">start() vs run()<\/strong><br \/>\nThread can be invoked using start or run method. If you use the <em>myThread.start()<\/em> method it initiates execution of new thread, jvm takes care of execution and scheduling of run method in separate concurrent context. Calling the run method directly <em>myThread.run()<\/em> will not start a new thread; it will execute the run method in the current thread (ie main thread).<\/p>\n<blockquote><p>\nstart method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place\n<\/p><\/blockquote>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Thread objThread = new Thread(() -&gt; {\r\n            System.out.println(Thread.currentThread().getName());\r\n        });\r\n\r\n        objThread.start();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThread-0\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Thread objThread = new Thread(() -&gt; {\r\n            System.out.println(Thread.currentThread().getName());\r\n        });\r\n\r\n        objThread.run();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nmain\r\n<\/pre>\n<ol>\n<li><strong class=\"ctaHeader3\">start()<\/strong> &#8211; Initiates the execution of the thread, causing the run method to be called. When you call the start() new thread would spawn and start execution other than main thread\n<p><strong>Task.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Task implements Runnable{\r\n    @Override\r\n    public void run() {\r\n        System.out.println(&quot;Hello from Task.class, executed by &quot; + currentThread().getName() + &quot; thread&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Main.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Thread thread = new Thread(new Task());\r\n        thread.start();\r\n        thread.run();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nHello from Task.class, executed by main thread\r\nHello from Task.class, executed by Thread-0 thread\r\n<\/pre>\n<\/li>\n<li><strong class=\"ctaHeader3\">run()<\/strong> &#8211; Contains the code that will be executed by the thread. This method needs to be overridden when extending the Thread class or implementing the Runnable interface. Usage: Defined by the user based on the specific task.<\/li>\n<li><strong strong class=\"ctaHeader3\">sleep(long milliseconds)<\/strong> &#8211; Causes the thread to sleep for the specified number of milliseconds, pausing its execution.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nThread.sleep(1000);\r\n<\/pre>\n<\/li>\n<li><strong class=\"ctaHeader3\">join()<\/strong> Waits for the thread to complete its execution before the current thread continues. It is often used for synchronization between threads.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">myThread.join();<\/pre>\n<\/li>\n<li><strong class=\"ctaHeader3\">interrupt()<\/strong> Interrupts the thread, causing it to stop or throw an InterruptedException. The thread must handle interruptions appropriately.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmyThread.interrupt();\r\n<\/pre>\n<\/li>\n<li><strong class=\"ctaHeader3\">isAlive()<\/strong> Returns true if the thread has been started and has not yet completed its execution, otherwise returns false.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nboolean alive = myThread.isAlive();\r\n<\/pre>\n<\/li>\n<li><strong class=\"ctaHeader3\">setName(String name)<\/strong> Sets the name of the thread.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmyThread.setName(&quot;MyThread&quot;);\r\n<\/pre>\n<\/li>\n<li><strong  class=\"ctaHeader3\">getName()<\/strong> Returns the name of the thread.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString threadName = myThread.getName();\r\n<\/pre>\n<\/li>\n<li><strong  class=\"ctaHeader3\">getName()<\/strong> Returns the name of the thread.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">String threadName = myThread.getName();\r\n<\/pre>\n<\/li>\n<li> <strong  class=\"ctaHeader3\">setPriority(int priority)<\/strong> Sets the priority of the thread. Priorities range from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmyThread.setPriority(Thread.MAX_PRIORITY);\r\n<\/pre>\n<\/li>\n<li> <strong  class=\"ctaHeader3\">getPriority()<\/strong> Returns the priority of the thread.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nint priority = myThread.getPriority();\r\n<\/pre>\n<\/li>\n<li><strong  class=\"ctaHeader3\">currentThread()<\/strong> Returns a reference to the currently executing thread object.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nThread currentThread = Thread.currentThread();\r\n<\/pre>\n<\/li>\n<\/ol>\n<p><strong>Thread Implementation Using Runnable Interface<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Threads2 implements Runnable \r\n{\t\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t\tThreads2 objThreads2 = new Threads2();\r\n\t\tThread t1 = new Thread(new Threads2());\r\n\t\tt1.start();\r\n\t}\r\n\t\r\n\tpublic void run() \r\n\t{\t\r\n\t\tSystem.out.println(&quot;Thread Started&quot;);\r\n\t\t\r\n\t\tfor(int i=0;i&lt;=5;i++)\r\n\t\t{\r\n\t\t\tSystem.out.println(i);\r\n\t\t\t\r\n\t\t\ttry \r\n\t\t\t{\r\n\t\t\t  Thread.sleep(1000);\r\n\t\t\t}\r\n\t\t\tcatch (InterruptedException e) \r\n\t\t\t{\r\n\t\t\t  e.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(&quot;Thread Stopped&quot;);\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p>There are Four Constructors we can use in Runnable Method of Implementation of Threads.<br \/>\n<strong>Thread()<\/strong> &#8211; Calls Thread Version of run Method<br \/>\n<strong>Thread(Runnable Target)<\/strong><br \/>\n<strong>Thread(Runnable Target, String Name) <\/strong><br \/>\n<strong>Thread(String Name) <\/strong><\/p>\n<p>If we dont Pass the Instance of Class To thread Constrctor then it will call its own Run Method<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nThread t = new Thread(new MyThread());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple Thread implementing Runnable Counter.java public class Counter implements Runnable{ public void run(){ System.out.println(&quot;I am Counter Run Method&quot;); } } Main.java public class Main { public static void main(String&#x5B;] args) { Counter objCounter = new Counter(); Thread objThread = new Thread(objCounter); objThread.start(); } } I am Counter Run Method Main.java(Using Lambda Expression) public class Main&hellip; <a href=\"https:\/\/codethataint.com\/blog\/threads-implementation-using-runnable\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,141],"tags":[],"class_list":["post-695","post","type-post","status-publish","format-standard","hentry","category-java","category-threads"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/695","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=695"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/695\/revisions"}],"predecessor-version":[{"id":5465,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/695\/revisions\/5465"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}