Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no system call being invoked for thread creation, when i am creating multiple threads in java?

The following code creates 100 new java threads and runs 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 records the system calls executed by this using strace, i cannot find any system call(maybe clone()) which is creating a new thread.

But when i check the threads for the above process using ps -eLf command then it lists(>100) threads with different thread ids.

How does these threads got created without any system call? And if jvm were creating threads in userspace then these shouldn't be listed by ps -eLf.

The output of 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)                           = ?

I have removed the initial system calls required for launching jvm. The only clone system call being shown is the one for creating the main thread.

like image 618
Sanjeev Kumar Avatar asked Jun 30 '14 07:06

Sanjeev Kumar


People also ask

How do you call multiple threads in Java?

In order to use multiple threads in Java, you need to first define the task which will be executed by those threads. In order to create those tasks, you can either use the Runnable or Callable interface.

Which of the system call is used to create a thread?

Posix thread system calls. #include <pthread. h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine, void*),void *arg); This system call has four arguments, the first *thread is a pointer to the thread id number (the type pthread_t is an int).

Do threads use system calls?

Threads spawned by a process are additional child processes of the main “thread's” parent process. They're manipulated through the same process management system calls, eliminating the need for a separate set of thread-related system calls.

Can you make multiple thread to execute same instructions?

In the same multithreaded process in a shared-memory multiprocessor environment, each thread in the process can run concurrently on a separate processor, resulting in parallel execution, which is true simultaneous execution.


2 Answers

On my machine running Oracle Java 8u5, I see very similar output from running just strace java ThreadTest. However, when I enable the -f flag to follow child processes, I see dozens of clone() calls being made.

Your invocation of strace is telling it just to pay attention to the process launched directly (the JVM bootstrap thread), and it spawns only the main thread. Then that thread is the one calling clone() to create all your workers.

like image 66
chrylis -cautiouslyoptimistic- Avatar answered Oct 23 '22 02:10

chrylis -cautiouslyoptimistic-


How does these threads got created without any system call?

There certainly is system call clone involved. Try strace with -f option, ref: man strace

 -f          Trace child processes as they are created by currently traced processes as a result of the fork(2) system call.

                   On non-Linux platforms the new process is attached to as soon as its pid is known  (through  the  return  value  of
                   fork(2)  in  the parent process). This means that such children may run uncontrolled for a while (especially in the
                   case of a vfork(2)), until the parent is scheduled again to complete its (v)fork(2) call.  On Linux  the  child  is
                   traced from its first instruction with no delay.  If the parent process decides to wait(2) for a child that is cur-
                   rently being traced, it is suspended until an appropriate child process either terminates or incurs a  signal  that
                   would cause it to terminate (as determined from the childâs current signal disposition).

And if jvm were creating threads in userspace then these shouldn't be listed by ps -eLf.

The threads created by jvm are user level threads represented by LWP (kernel level abstraction) on linux.

see the lwp (native thread id) & nlwp columns in ps -eLF result.

like image 2
Shail016 Avatar answered Oct 23 '22 03:10

Shail016