From d30678af2bad4c61a193c0c60b95a7038f3dc546 Mon Sep 17 00:00:00 2001 From: Archit Singla Date: Tue, 16 Oct 2018 01:41:34 +0530 Subject: [PATCH] feat(index.md) :- Added Thread lifecycle (#18773) Added Thread lifecycle plus removed non working url --- .../english/java/multithreading/index.md | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/client/src/pages/guide/english/java/multithreading/index.md b/client/src/pages/guide/english/java/multithreading/index.md index 7d8f749c44..42a04015d8 100644 --- a/client/src/pages/guide/english/java/multithreading/index.md +++ b/client/src/pages/guide/english/java/multithreading/index.md @@ -4,7 +4,24 @@ title: Multithreading ## Multithreading -Multithreading is a process of executing multiple processes simultaneously. Java starts the program with a main thread and further threads are added upon main thread whenever any user creates it. main thread is the first user thread in any Java program. Also, JVM makes sure that all the user threads are closed before the program ends. +Multithreading is a process of executing multiple processes simultaneously. Java starts the program with a main thread and further threads are added upon main thread whenever any user creates it. Main thread is the first user thread in any Java program. Also, JVM makes sure that all the user threads are closed before the program ends. + +## Thread LifeCycle + +A thread at any of point exists in any one of the following states: + +1. New - When a new thread is created, it is in the new state. The thread has not started yet. + +2. Runnable - A thread that is ready to run is moved to runnable state. + +3. Running - The thread scheduler picks up a thread from runnable threads pool and ensures that each thread gets to run. + +4. Waiting/Blocked - When a thread is temporarily inactive, then is in blocked/waiting state. Example - a thread waiting for I/O to complete. + +5. Terminated - A thread can be terminated due to 2 reasons. Either it has completed the job and terminates normally or due to some unhandled exception or segmentation fault, etc. + +For more info on thread states, refer :- https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/ + A thread has both advantages and disadvantages. @@ -31,6 +48,6 @@ This class also contains public void run() which we need to override in order to The code for both can be found here: http://ide.geeksforgeeks.org/k7GjcA. -You will notice that if this code is ran multiple times, the results may differ. and that is decided by the OS upon which it is run. The OS can pick any thread from a runnable state and can run it. We hsve NO CONTROL over that. If there are multiple threads in runnable state (ready to run), anyone can be picked. It even does not depend upon priority. +You will notice that if this code is ran multiple times, the results may differ. and that is decided by the OS upon which it is run. The OS can pick any thread from a runnable state and can run it. We have NO CONTROL over that. If there are multiple threads in runnable state (ready to run), anyone can be picked. It even does not depend upon priority. + -Further details: https://www.ntu.edu.sg/home/ehchua/programming/java/J5e_multithreading.html