Java Threads vs Pthreads

前端 未结 2 1788
深忆病人
深忆病人 2021-01-30 22:21

I was asked this question in an interview today.

\"When we create a thread with pthread_create() (POSIX Threads), the thread starts on its own. Why do we n

2条回答
  •  孤街浪徒
    2021-01-30 23:07

    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.

提交回复
热议问题