The following code creates 100 new java threads and starts them.
class ThreadTest { public static void main(String[] args) { for (int i = 0; i < 100; i++) { final int tNo = i; new Thread(new Runnable() { @Override public void run() { System.out.println("thread #" + tNo); } }).start(); } } }
When I run the above code and record system calls made with strace, I cannot find the system call (possibly clone ()) that creates a new thread.
But when I check the threads for the above process with the ps -eLf command, then it lists (> 100) threads with different thread IDs.
How are these threads created without any system call? And if jvm created threads in user space, then they should not be listed ps -eLf.
The output of the strace command
mprotect(0xf95000, 8876032, PROT_READ|PROT_EXEC) = 0 munmap(0xf7762000, 92395) = 0 mmap2(NULL, 331776, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0xfffffffff770f000 mprotect(0xf770f000, 4096, PROT_NONE) = 0 clone(child_stack=0xf775f494, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0xf775fbd8, tls=0xf775fbd8, child_tidptr=0xffdb53d0) = 31692 futex(0xf775fbd8, FUTEX_WAIT, 31692, NULLthread #1 thread #5 thread #4 thread #3 ..... ) = 0 exit_group(0) = ? CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID, parent_tidptr = 0xf775fbd8, tls = 0xf775fbd8, child_tidptr = 0xffdb53d0) = mprotect(0xf95000, 8876032, PROT_READ|PROT_EXEC) = 0 munmap(0xf7762000, 92395) = 0 mmap2(NULL, 331776, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0xfffffffff770f000 mprotect(0xf770f000, 4096, PROT_NONE) = 0 clone(child_stack=0xf775f494, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0xf775fbd8, tls=0xf775fbd8, child_tidptr=0xffdb53d0) = 31692 futex(0xf775fbd8, FUTEX_WAIT, 31692, NULLthread #1 thread #5 thread #4 thread #3 ..... ) = 0 exit_group(0) = ?
I removed the initial system calls needed to start jvm. The only clone system call displayed is the one that creates the main thread.