2

I have a questions about Java thread and OS thread, and I read Java Threads vs Pthreads and Java Threads vs OS Threads but I does not find an answer to my confusions.

I had considered before we call start(), no OS thread is created .

But according to https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html ,

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

Am I wrong or the states defined at java doc are not only to mark the state of Thread instance?

My question is :

  • what exactly happens when we create a Thread instance but before calling start()
  • when does a OS thread really created in Java
Community
  • 1
  • 1
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149

1 Answers1

3
Thread thread = new Thread(){
    @Override
    public void run() {
        // code
    }
}; 
// at this point the thread is in NEW state, all you have a simple java object, 
// no actual thread is created

thread.start();
// when start() is invoked, at some unspecified point in the near future 
// the thread will go into RUNNABLE state, this means an actual thread will be created.  
// That can happen before start() returns.
outdev
  • 5,249
  • 3
  • 21
  • 38