Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java threads created in user space or kernel space?

Check this code out

    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() 
        {
            try
            {
                System.out.println("STARTING SERVER...");
                ServerSocket s = new ServerSocket(2544);
                System.out.println("SERVER BLOCKED ON ACCEPT");
                Socket ss = s.accept();
                System.out.println("SERVER NOT BLOCKED ANYMORE");
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });
    t1.start();



    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() 
        {
            try
            {
                while(true)
                {
                    Thread.sleep(1000);
                    System.out.println("Hello");
                }
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });
    t2.start();

Outputs:

STARTING SERVER...
SERVER BLOCKED ON ACCEPT
Hello
Hello
Hello
Hello
Hello
Hello
Hello
...

Java threads should be user space threads, right? So one blocked thread should block the entire process...thats not what happened. What is happening them?

like image 868
fredcrs Avatar asked Aug 16 '13 16:08

fredcrs


People also ask

Are Java threads user level or kernel-level?

Java threads are user threads for sure but eventually they're mapped to kernel-level threads before getting executed for real. This mapping probably depends on particular JVM implementation (or the mapping model).

Does Java use kernel threads?

Java on Solaris software leverages the multithreading capabilities of the kernel while also allowing you to create powerful Java applications using thousands of user-level threads for multiprocessor or uniprocessor systems, through a very simple programming interface.

Do user level threads run in user space?

User level threads are supported above the kernel in user space and are managed without kernel support. Threads managed entirely by the run-time system (user-level library). Ideally, thread operations should be as fast as a function call.

How are threads implemented in user space?

Step 1 − The complete thread package is placed in the user space and the kernel has no knowledge about it. Step 2 − Kernel generally, manages ordinary and single threaded processes. Step 3 − Threads are always run on top of a run-time system. Step 4 − Run time system is a collection of procedures which manage threads.


1 Answers

Java threads are "user" threads, but under the hood, the Java Virtual Machine is using kernel threads and delegating the user threads CPU time on each kernel thread in its kernel thread pool. See this question for a better explanation. It seems that threading is JVM-vendor specific, and my understanding might not hold for all JVM implementations.

like image 178
Samuel Avatar answered Oct 21 '22 17:10

Samuel