0
public class ThreadState{
    public static void main(String[] args){
        Thread one = new Thread(new Runnable(){
            @Override
            public void run(){
                System.out.println(Thread.currentThread().getName() + " " + Thread.currentThread().getState());
            }
        });
        Thread two = new Thread(new Runnable(){
            @Override
            public void run(){
                System.out.println(Thread.currentThread().getName() + " " + Thread.currentThread().getState());
            }
        });
        System.out.println(one.getState());
        System.out.println(two.getState());

        one.start();
        two.start();

        System.out.println(one.getState());
        System.out.println(two.getState());
        System.out.println(one.getState());
        System.out.println(two.getState());

        System.out.println("Main ");
    }
}

I'm trying to learn Thread.State in java but I'm confusing between them. When I run the above code I got multiple results ( I know because of Multithreading ).

But I'm unable to understand state of them.

1) Here two outputs which I want to learn can you explain any one them?

NEW
NEW
RUNNABLE
RUNNABLE
RUNNABLE
RUNNABLE
Thread-0 RUNNABLE
Thread-1 RUNNABLE
Main

The second one is this.

NEW 
NEW 
RUNNABLE 
BLOCKED
BLOCKED // 2) Why go to again BLOCKED state? as before it was in RUNNABLE state
BLOCKED
Main
Thread-0 RUNNABLE
Thread-1 RUNNABLE

I already know about what is the NEW state, What is the RUNNABLE state. But I'm confused about BLOCKED and WAITING state.

Student object1 = new Student(); // any shared resource

Thread t1 = new Thread(/*......*/); => NEW State
Thread t2 = new Thread(/*......*/); => NEW State

t1.start(); => RUNNABLE STATE
t2.start(); => RUNNABLE STATE

`t1` and `t2` trying to get lock on object1 and `t1` successfully got locked on object1.

3) Now t2 will be in BLOCKED State or WAITING State?

`t1` called `wait()` method and released lock.
`t2` got the lock and `t1` now in WAITING state.

`t2` called `notify();` and released lock.
`t1` got the lock 

4) t2 will be in BLOCKED State or Waiting State?

`t1.run();` finished => TERMINATED State
`t2.run();` finished => TERMINATED State

5) JVM keep history of each thread even after thread's dead? If no then how JVM show TERMINATED state of thread that is already gone?

3 Answers3

1

BLOCKED means: "A thread that is blocked waiting for a monitor lock is in this state." This means there is synchronization involved. You aren't using any synchronized blocks but the Thread does internally. Given it was RUNNING and then BLOCKED, it's probably synchronization that happens in the private Thread.exit() method. Group.threadTerminated() is synchronized on the group, for example.

WAITING means that the thread has called Object.wait(). In order to do this, it has to own the monitor (associated with the Object wait was called on) so it cannot be BLOCKED on that monitor.

For 3 & 4 if I follow, the answer is 3) BLOCKED and 4) RUNNING or TERMINATED

As to whether the JVM holds on the history, I'm not sure that's a requirement of the JVM but as long as you have a reference to the Thread Object, you should be able to see what the last state was. Keep in mind that the Thread object is just a representation of the thread as an Object. The actual thread is OS/hardware specific.

JimmyJames
  • 1,356
  • 1
  • 12
  • 24
  • Plain guesse (dont know for sure). But java Thread instance still exist, but the real Thread is gone. Result is TERMINATED. – n247s May 06 '16 at 16:40
0

1) Here two outputs which I want to learn can you explain any one them? I already know about what is the NEW state, What is the RUNNABLE state. But I'm confused about BLOCKED and WAITING state.

The states are explained here.

3) Now t2 will be in BLOCKED State or WAITING State?

In the situation you describe, t2 should be in RUNNABLE state, IMHO. It has released the lock, i. e. left the synchronized block surrounding the notify() call.

4) t2 will be in BLOCKED State or Waiting State?

I don't get this one - it should be in the TERMINATED state as soon as it is really terminated.

5) JVM keep history of each thread even after thread's dead? If no then how JVM show TERMINATED state of thread that is already gone?

Even if the thread is gone, the object controlling the thread is available as long as you hold a reference to it. And this object tells you that the thread is TERMINATED.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
glglgl
  • 89,107
  • 13
  • 149
  • 217
0

1) Here two outputs which I want to learn can you explain any one them?

Refer to thread states article for better understanding & various conditions that result into changes in thread states.

A thread can be in one of the following states:

NEW :A thread that has not yet started is in this state.

RUNNABLE :A thread executing in the Java virtual machine is in this state.

BLOCKED: A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

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

A thread is in the waiting state due to calling one of the following methods:

Object.wait with no timeout
Thread.join with no timeout
LockSupport.park

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

A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

Thread.sleep
Object.wait with timeout
Thread.join with timeout
LockSupport.parkNanos
LockSupport.parkUntil

TERMINATED: A thread that has exited is in this state.

2) Why go to again BLOCKED state? as before it was in RUNNABLE state

A thread is blocked waiting for a monitor lock is in this state.

For queries 3 and 4 : If a thread (t1) release the lock, other waiting thread (t2) will enter from BLOCKED state to RUNNABLE state. t1 thread will enter TERMINATED state if it has completed it's execution. If t2 completes execution, it will enter into TERMINATED state.

5) JVM keep history of each thread even after thread's dead? If no then how JVM show TERMINATED state of thread that is already gone?

Since your program holds the reference of threads, you are getting TERMINATED state for those threads. If you free up these references, JVM don't have any history of these threads

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211