{"id":875,"date":"2016-02-08T09:00:47","date_gmt":"2016-02-08T16:00:47","guid":{"rendered":"http:\/\/gogotraining.com\/blog\/?p=875"},"modified":"2016-01-22T22:41:54","modified_gmt":"2016-01-23T05:41:54","slug":"getting-acquainted-with-a-java-thread","status":"publish","type":"post","link":"https:\/\/gogotraining.com\/blog\/2016\/02\/getting-acquainted-with-a-java-thread\/","title":{"rendered":"Getting Acquainted with a Java Thread"},"content":{"rendered":"<p class=\"p1\"><a href=\"http:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/02\/Getting-Acquainted-with-a-Java-Thread-V-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-891\" src=\"http:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/02\/Getting-Acquainted-with-a-Java-Thread-V-1.jpg\" alt=\"Getting-Acquainted-with-a-Java-Thread-V-1\" width=\"700\" height=\"400\" srcset=\"https:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/02\/Getting-Acquainted-with-a-Java-Thread-V-1.jpg 700w, https:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/02\/Getting-Acquainted-with-a-Java-Thread-V-1-300x171.jpg 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p class=\"p1\"><span class=\"s1\">Multithreaded programs can be developed using the Java Programming language.<span class=\"Apple-converted-space\">\u00a0 <\/span>By a multithread program, it means a program comprises of two or more actions, where each action performs different tasks simultaneously. Multithreading can also be said as a synonym for multitasking where each thread corresponds to each task. The benefits of multithreaded program are that multiple activities run parallel and use the same program. <\/span><\/p>\n<p class=\"p3\"><span class=\"s1\"><b>What is a Thread?<\/b><\/span><\/p>\n<p class=\"p2\"><span class=\"s1\">A thread in a Java program in its simplest definition is a sequential flow of statements that defines an execution path for a program.<span class=\"Apple-converted-space\">\u00a0 <\/span>A Java program should contain at least one thread that is the main thread, which is invoked by the main () method. <\/span><\/p>\n<p class=\"p3\"><span class=\"s1\"><b>Ways to Create a Java Thread<\/b><\/span><\/p>\n<p class=\"p4\"><span class=\"s1\">The Thread class is one of the main classes in Java that is written as java.lang.Thread class. This class, along with the Runnable interface, is used to create threads in Java. A thread in Java can be created in either of the two following ways:<\/span><\/p>\n<ul>\n<li class=\"li4\"><span class=\"s1\">Extending the java.lang.Thread class<\/span><\/li>\n<\/ul>\n<p class=\"p4\"><span class=\"s1\">Or <\/span><\/p>\n<ul>\n<li class=\"li2\"><span class=\"s1\">Implementing the java.lang.Runnable Interface<\/span><\/li>\n<\/ul>\n<p class=\"p3\"><span class=\"s1\"><b>Life Cycle of a Java Thread<\/b><\/span><\/p>\n<p class=\"p4\"><span class=\"s1\">A thread in a Java program goes through different states, as shown in the following flowchart:<\/span><\/p>\n<p class=\"p4\"><a href=\"http:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/01\/Screen-Shot-2016-01-22-at-8.54.54-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-876 alignnone\" src=\"http:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/01\/Screen-Shot-2016-01-22-at-8.54.54-PM.png\" alt=\"Screen Shot 2016-01-22 at 8.54.54 PM\" width=\"419\" height=\"381\" srcset=\"https:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/01\/Screen-Shot-2016-01-22-at-8.54.54-PM.png 526w, https:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/01\/Screen-Shot-2016-01-22-at-8.54.54-PM-300x273.png 300w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/a><a href=\"http:\/\/gogotraining.com\/blog\/wp-content\/uploads\/2016\/01\/Getting-Acquainted-with-a-Java-Thread-V-1.png\"><br \/>\n<\/a><\/p>\n<p class=\"p4\"><span class=\"s1\">A brief description of these states is as follows:<\/span><\/p>\n<ul>\n<li class=\"li4\"><span class=\"s1\"><b>New:<\/b><span class=\"Apple-converted-space\">\u00a0 <\/span>Refers to the state after an instance of the Thread class gets created but before the invocation of the start () method. This state marks the beginning of life cycle of a thread. <\/span><\/li>\n<li class=\"li4\"><span class=\"s1\"><b>Runnable:<\/b> Refers to the state where a thread executes the task for which it is created. The thread comes into this state after the start() method is invoked. <\/span><\/li>\n<li class=\"li4\"><span class=\"s1\"><b>Running:<\/b> Refers to the state where thread is selected by the scheduler for the execution purpose.<b> <\/b><\/span><\/li>\n<li class=\"li4\"><span class=\"s1\"><b>Not-Runnable (Blocked):<\/b> Refers to the state where thread is alive; however it is not eligible to run. <\/span><\/li>\n<li class=\"li2\"><span class=\"s1\"><b>Terminated:<\/b> Refers to the state when a thread gets terminate and is in a dead state. <\/span><\/li>\n<\/ul>\n<p class=\"p3\"><span class=\"s1\"><b>Familiarizing with Thread Priorities<\/b><\/span><\/p>\n<p class=\"p4\"><span class=\"s1\">We are aware of the availability of multiple Java threads in a program. But the question that arises is how a program knows which thread to run first or sequence of execution order for all threads. The solution to this can be given by scheduling thread execution. Each thread in a Java program has a priority. By default, priority of a thread is inherited by its parent thread and is in between numbers 1 to 10, where number 1 denotes that thread is at the minimum priority level and 10 denotes the highest priority level for a thread. Further, thread priority can also be changed according to the requirement using the java.lang.Thread.setPriority () method.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Multithreaded programs can be developed using the Java Programming language.\u00a0 By a multithread program, it means a program comprises of two or more actions, where each action performs different tasks simultaneously. Multithreading can also be said as a synonym for multitasking where each thread corresponds to each task. The benefits of multithreaded program are that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-875","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/posts\/875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/comments?post=875"}],"version-history":[{"count":3,"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/posts\/875\/revisions"}],"predecessor-version":[{"id":893,"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/posts\/875\/revisions\/893"}],"wp:attachment":[{"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/media?parent=875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/categories?post=875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gogotraining.com\/blog\/wp-json\/wp\/v2\/tags?post=875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}