Java Threads vs Pthreads

In Java not starting the thread right away leads to a better API. You can set properties on the thread (daemon, priority) without having to set all the properties in the constructor.

If the thread started right away, it would need a constructor,

public Thread(Runnable target, String name, ThreadGroup threadGroup, int priority, boolean daemon, ContextClassLoader contextClassLoader, long stackSize)

To allow setting all these parameters before the thread started. The daemon property can't be set after the thread has started.

I'm guessing that the POSIX API takes a struct with all the thread properties in the call to pthread_create(), so it makes sense to start the thread right away.


The reasons are a lot. But I'll give you a few:

  • The thread, itself, might start executing before returning the instance.
  • The context classloader MUST be set properly before running the thread (look at the previous point)
  • Extra configuration like priority should be set before starting the thread
  • pthreads uses a pointer to the initialized structure(s), since the java.lang.Thread cannot be properly initialized in the end of the c-tor, see points above; straight call to the native pthread_create to actually execute the code makes no sense

I hope you get the idea.