top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is Thread Life cycle in Java please explain?

+1 vote
467 views
What is Thread Life cycle in Java please explain?
posted Dec 15, 2014 by Dominic

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

The life cycle of threads in Java is very similar to the life cycle of processes running in an operating system. During its life cycle the thread moves from one state to another depending on the operation performed by it or performed on it

A Java thread can be in one of the following states:

NEW: A thread that is just instantiated is in new state. When a start () method is invoked, the thread moves to the ready state from which it is automatically moved to runnable state by the thread scheduler.

RUNNABLE (ready_running) A thread executing in the JVM is in running state.

BLOCKED A thread that is blocked waiting for a monitor lock is in this state. This can also occur when a thread performs an I/O operation and moves to next (runnable) state.

WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.

TIMED_WAITING (sleeping) A thread that is waiting for another thread to perform an action up to a specified waiting time is in this state.

TERMINATED (dead) A thread that has exited is in this state.

answer Dec 16, 2014 by Karthick.c
Similar Questions
+2 votes
import java.util.ArrayList;
import java.util.List;

public class ThreadB extends Thread {
    private static List<String> list1   = new ArrayList<String>();
    private static List<String> list2   = new ArrayList<String>();
    private static final Object mutex   = new Object();

    public void run() {
        try {
            sleep(2000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (mutex) {
            list1.add("Hello");
            System.out.println("Added for 1   " + list1.size());
            list2.add("Added for 2");
            System.out.println("Added for 2   " + list2.size());
            mutex.notify();
        }
    }

    public static void main(String[] args) {
        ThreadB a1 = new ThreadB();
        ThreadB a2 = new ThreadB();
        a1.start();
        a2.start();
        new ThreadB().m1();    
    }

    private void m1() {         
        synchronized (list1) {
            System.out.println(list1.size());
            System.out.println(list2.size());
            try {
                list1.add("hello");
                System.out.println("m1  :::   " + list1.size());
                sleep(5000l);
                System.out.println("Oh mY God");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

Output is

0
0
m1  :::   1
Added for 1   2
Added for 2   1
Added for 1   3
Added for 2   2
Oh mY God

After locking list1, how other threads are going to access that list?

+1 vote

As JVM uses the primitive or time slicing scheduling techniques for thread scheduling. Is it possible to know our exiting JVM using which technique for thread scheduling?

...