The Artima Developer Community
Sponsored Link

Java Buzz Forum
Life cycle of thread in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Life cycle of thread in java Posted: Jul 25, 2015 1:23 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Life cycle of thread in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Its recommended to learn about life cycle of Thread before you start programming on Thread.
  • Threads exists in different type of states.

  • Thread having below states.
  1. New State
  2. Ready State
  3. Running State
  4. Dead State
  5. Non Runnable States 
Life cycle of a Thread

    1.New State:

    • A thread has been created but not started yet. A thread will be started by calling its start() method.

    2.Runnable State:

    • This state is also called ready to run stage also called queue. A thread starts in runnable state by calling start() method.
    • The Thread scheduler decides which thread runs and how long.

    3.Running State:

    • If a Thread is executing that means Thread is in Running stage.

    4.Dead State:

    • Once a Thread reached dead state it can not run again.

    5. Non runnable States:

    • A Running Thread transit to one of the non runnable states, depending upon the circumstances.
    • A Thread remains non runnable until a special transition occurs.
    • A Thread does not go directly to the running state from non runnable state.
    • But transits first to runnable state.
    1. Sleeping: The Threas sleeps for specified amount of time.
    2. Blocked for I/O: The Thread waits for a blocking operation to complete.
    3. Blocked for join completion: The Thread waits for completion of another Thread.
    4. Waiting for notification: The Thread waits for notification another Thread.
    5. Blocked for lock acquisition: The Thread waits to acquire the lock of an object.
    • JVM executes the Thread based on their priority and scheduling.

    Thread Scheduler:

    • Schedulers in JVM implementations usually employ one of these two Strategies.
    • Preemptive Scheduling
    • Time Sliced or Round robin Scheduling
    • Thread schedulers are implementation and platform independent, therefore how thread will scheduled is unpredictable

    Thread priority:

    • JVM will assign a priority for every Thread created in it.
    • 0- will be the minimum priority
    • 5- will be the normal priority
    • 10- will be the maximum priority
    • To hold all these values Thread class has below three corresponding variables
    • public static final int MIN_PRIORITY
    • public static final int NORM_PRIORITY
    • public static final int MAX_PRIORITY 
    •  A thread inherits the priority of its parent Thread. The default priority of the every thread is normal priority 5, because main thread priority is 5.
    • We can set the priority of a thread by using setPriority(int priority) method
    • public final void setPriority(int priority)
    • public void getPriority();
    • User defined thread created with default name  Thread+<index>, where index is the integer number starts from 0.
    • The name of a thread can be change using setName(String name) method.
    • Get by using getName() method.
    • public final void setName(String name)
    • public final String getName().



    1. package com.instanceofjava;
    2.  
    3. class UserThread{
    4.      
    5. UserThread(){
    6.         super();
    7.  }
    8.  
    9.  UserThread(String name){
    10.         UserThread(name);        
    11.  }
    12.  
    13.  public void run()
    14.  {
    15.       System.out.println("thread started running..");
    16.  }
    17.  
    18. public static void main(String [] args){ 

    19.   UserThread thread1 = new UserThread("Thread1");
    20.   UserThread thread2 = new UserThread("Thread2");        

    21.          System.out.println("Thread 1 initial name and priority");
    22.          System.out.println("name:"+thread1.getName());
    23.          System.out.println("priority:"+thread1.getPriority());
    24.  
    25.       System.out.println("Thread 2 initial name and priority");
    26.       System.out.println("name:"+thread2.getName());
    27.       System.out.println("priority:"+thread2.getPriority());
    28.  
    29.       thread1.setPriority(6);
    30.       thread2.setPriority(9);
    31.  
    32.       System.out.println("Thread 1 initial name and priority");
    33.       System.out.println("name:"+thread1.getName());
    34.       System.out.println("priority:"+thread1.getPriority())
    35.  
    36.       System.out.println("Thread 2 initial name and priority");
    37.       System.out.println("name:"+thread2.getName());
    38.       System.out.println("priority:"+thread2.getPriority());
    39.  
    40.       thread1.start();
    41.       thread2.start();
    42.  
    43.       for (int i = 0; i < 5; i++) {
    44.           System.out.println("main method i value:"+i);
    45.     }
    46. }
    47. }


    Output:

    1. Thread 1 initial name and priority
    2. name:Thread1
    3. priority:5
    4. Thread 2 initial name and priority
    5. name:Thread2
    6. priority:5
    7. Thread 1 initial name and priority
    8. name:Thread1
    9. priority:6
    10. Thread 2 initial name and priority
    11. name:Thread2
    12. priority:9
    13. Thread1i:0
    14. Thread1i:1
    15. Thread1i:2
    16. Thread2i:0
    17. Thread1i:3
    18. Thread1i:4
    19. Thread2i:1
    20. Thread2i:2
    21. Thread2i:3
    22. Thread2i:4
    23. main method i value:0
    24. main method i value:1
    25. main method i value:2
    26. main method i value:3
    27. main method i value:4

      Read: Life cycle of thread in java

      Topic: Building a Scalable and Resilient Architecture Previous Topic   Next Topic Topic: Board Games 1: Family Games

      Sponsored Links



      Google
        Web Artima.com   

      Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use