{"id":1331,"date":"2016-04-26T15:49:27","date_gmt":"2016-04-26T15:49:27","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1331"},"modified":"2016-04-26T15:50:11","modified_gmt":"2016-04-26T15:50:11","slug":"creating-a-thread","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/creating-a-thread.html","title":{"rendered":"Creating a Thread in Java with Example"},"content":{"rendered":"<p>Threads as we have discussed are light- weight smallest part of process that can perform tasks simultaneously with other threads of the same process. In this article we will discuss how to create thread with examples in Java.<\/p>\n<p>There are two ways by which thread can be created in Java:<\/p>\n<ol>\n<li>By extending Thread class<\/li>\n<li>By implementing Runnable interface.<\/li>\n<\/ol>\n<hr \/>\n<h4><strong>Creating Thread By extending Thread class<\/strong><\/h4>\n<p>First way of creating a multithreaded program is by extending Thread class. Thread class extends the Object class and implements Runnable interface. The Thread class provides many Constructors and methods that are used to do various operations on created threads. These constructors and methods are discussed below.<\/p>\n<p><span style=\"color: #008000;\"><strong>Constructors of Thread class<\/strong><\/span><\/p>\n<p><strong><span style=\"color: #008000;\">1. Thread()<\/span><\/strong><\/p>\n<p>This constructor creates a thread object having no predefined arguments.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. Thread(String name)<\/strong><\/span><\/p>\n<p>This constructor creates a thread object having String argument.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. Thread(Runnable r)<\/strong><\/span><\/p>\n<p>This constructor creates a thread object having argument r of type runnable<\/p>\n<p><span style=\"color: #008000;\"><strong>4. Thread(Runnable r,String name)<\/strong><\/span><\/p>\n<p>This constructor creates a thread object having two arguments of String and Runnable type.<\/p>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Methods of Thread class:<\/strong><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong>1. public void run():<\/strong><\/span><\/p>\n<p>This method is used to perform needed action for a thread. This method is called by JVM after start() method is called.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. public void start():<\/strong><\/span><\/p>\n<p>This method is called by thread object to start the execution of the thread. After this method is called, JVM automatically calls the run() method.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. public void sleep(long milliseconds):<\/strong><\/span><\/p>\n<p>This method\u00a0ceases the execution of currently executing thread for the time period as specified.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. public void join():<\/strong><\/span><\/p>\n<p>This method causes the current thread (which calls this method) to wait until another thread to die.<\/p>\n<p><span style=\"color: #008000;\"><strong>5. public void join(long miliseconds):<\/strong><\/span><\/p>\n<p>This method causes the current thread (which calls this method) to wait for a thread to die for the specified miliseconds.<\/p>\n<p><span style=\"color: #008000;\"><strong>6. public int getPriority():<\/strong><\/span><\/p>\n<p>This method\u00a0returns the priority (integer value) of the thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>7. public int setPriority(int priority):<\/strong><\/span><\/p>\n<p>This method\u00a0sets the priority of the thread. The priority is in terms of integer value from 1 to 10. Priority increases with integer value from 1 being lowest and 10 being highest.<\/p>\n<p><span style=\"color: #008000;\"><strong>8. public String getName():<\/strong><\/span><\/p>\n<p>This method\u00a0returns the name of the calling thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>9. public void setName(String name):<\/strong><\/span><\/p>\n<p>This method changes the name of the calling thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>10. public Thread currentThread():<\/strong><\/span><\/p>\n<p>This method returns the reference of current thread which is executing.<\/p>\n<p><span style=\"color: #008000;\"><strong>11. public int getId():<\/strong><\/span><\/p>\n<p>This method returns the id of the calling thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>12. public Thread.State getState():<\/strong><\/span><\/p>\n<p>This method returns the state of the calling thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>13. public boolean isAlive():<\/strong><\/span><\/p>\n<p>This method returns true if the thread is alive, else returns false.<\/p>\n<p><span style=\"color: #008000;\"><strong>14. public void yield():<\/strong><\/span><\/p>\n<p>This method pauses the currently executing thread temporarily and allows other threads to execute.<\/p>\n<p><span style=\"color: #008000;\"><strong>15. public void suspend():<\/strong><\/span><\/p>\n<p>This method\u00a0is used to put the thread in suspended state.<\/p>\n<p><span style=\"color: #008000;\"><strong>16. public void resume():<\/strong><\/span><\/p>\n<p>This method is used to resume the suspended thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>17. public void stop():<\/strong><\/span><\/p>\n<p>This method\u00a0is used to stop the running thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>18. public boolean isDaemon():<\/strong><\/span><\/p>\n<p>This method returns true if the thread is a daemon thread, else returns false.<\/p>\n<p><span style=\"color: #008000;\"><strong>19. public void setDaemon(boolean b):<\/strong><\/span><\/p>\n<p>This method marks the thread as daemon or user thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>20. public void interrupt():<\/strong><\/span><\/p>\n<p>This method\u00a0interrupts the running thread.<\/p>\n<p><span style=\"color: #008000;\"><strong>21. public boolean isInterrupted():<\/strong><\/span><\/p>\n<p>This method returns true if the thread has been interrupted, else return false.<\/p>\n<p><span style=\"color: #008000;\"><strong>22. public static boolean interrupted():<\/strong><\/span><\/p>\n<p>This method returns true if the current thread has been interrupted, else return false.<\/p>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Example of Creating Thread by extending Thread class<\/strong><\/span><\/p>\n<p>In the following example we have created a class that extends Thread class, we have divided this program into two steps that are discussed below:<\/p>\n<p><strong>Step 1:<\/strong> In this step run() method of Thread class is overrided. This method provides the starting point for the program and the core logic is written in this method.<\/p>\n<p><strong>Step 2:<\/strong> In this step, once Thread is created start() method is called which automatically calls the run() method.<\/p>\n<pre>public class MultithreadingDemo extends Thread{\r\n\r\n\/\/Step 1: Overriding the run() method\r\n\r\npublic void run(){\r\n\r\nSystem.<em>out<\/em>.println(\" This is Multithreading . . . \");\r\n\r\n}\r\n\r\npublic static void main(String args[]){\r\n\r\nMultithreadingDemo thread1=new MultithreadingDemo();\r\n\r\n\/\/Step 2: calling the start() method\r\n\r\nthread1.start();\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>This is Multithreading . . .\r\n<\/pre>\n<hr \/>\n<h4><strong>Creating Thread By implementing Runnable interface<\/strong><\/h4>\n<p><strong>\u00a0<\/strong>Second way of creating a multithreaded program is by implementing runnable interface. This class provides only one method:<\/p>\n<p><span style=\"color: #008000;\"><strong>public void run()<\/strong><\/span><\/p>\n<p>This method is basically similar to method of Thread class, which is used to perform the basic action for a thread, core logic is written in this method.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example of creating thread by implementing Runnable Interface.<\/strong><\/span><\/p>\n<p><strong>\u00a0<\/strong>In the following example we have created a class that implements Runnable interface, we have divided this program into three steps that are discussed below:<\/p>\n<p><strong>Step 1:<\/strong> In this step run() method of Thread class is overrided. This method provides the starting point for the program and the core logic is written in this method.<strong>\u00a0<\/strong><\/p>\n<p><strong>Step 2:<\/strong> In this step, Object of Thread class is created. That passes the object of that class which implements the Runnable interface.<\/p>\n<p><strong>Step 3:<\/strong> In this step, once Thread is created start() method is called which automatically calls the run() method.<\/p>\n<pre>public class MultithreadingDemo implements Runnable{\r\n\r\n\/\/Step 1: overriding run() method\r\n\r\npublic void run(){\r\n\r\nSystem.<em>out<\/em>.println(\"this is multithreading...\");\r\n\r\n}\r\npublic static void main(String args[]){\r\n\r\nMultithreadingDemo multiDemo=new MultithreadingDemo();\r\n\r\n\/\/Step 2: Creating object of Thread class\r\n\r\nThread thread1 =new Thread(multiDemo);\u00a0\r\n\r\n\/\/Step 3: Calling start method\r\n\r\nthread1.start();\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>\u00a0<\/strong>Output:<\/span><\/p>\n<pre>this is multithreading...\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Threads as we have discussed are light- weight smallest part of process that can perform tasks simultaneously with other threads of the same process. In this article we will discuss how to create thread with examples in Java. There are two ways by which thread can be created in Java: By extending Thread class By &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/creating-a-thread.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Creating a Thread in Java with Example<\/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":[1,81],"tags":[],"class_list":["post-1331","post","type-post","status-publish","format-standard","hentry","category-archieve","category-thread"],"psp_head":"<title>Creating a Thread in Java with Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Here are two ways by which thread can be created in Java with example: by extending Thread class and by implementing Runnable interface.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/creating-a-thread.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1331"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1331\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}