13

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?

fredcrs
  • 3,558
  • 7
  • 33
  • 55
  • 2
    What's the point of multithreading if one blocked thread blocks the entire process? – Matt Ball Aug 16 '13 at 16:54
  • User space threads consume less resources since they do not have to lod program counter, stack, etc... of the kernel thread – fredcrs Aug 16 '13 at 16:57
  • @fredcrs yes, but they don't actually work at all well. – Martin James Aug 16 '13 at 17:16
  • maybe they can be usefull when both theads are 100% CPU bound. So kernel would not spend time switching context – fredcrs Aug 16 '13 at 17:20
  • Context-switching overhead only becomes noticeable under rare circumstances - more ready threads than cores and those threads use so much data that much of the L1 cache needs to be flushed. Not all them many apps like that. – Martin James Aug 16 '13 at 17:54

4 Answers4

21

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.

Community
  • 1
  • 1
Samuel
  • 16,923
  • 6
  • 62
  • 75
7

Most JVMs implement threads with native, OS level threads, including the Oracle reference implementation based on OpenJDK.

I imagine the JVMs that use 'green threads' (user space simulation of threads) would use preemptive scheduling so that an infinite loop in one thread doesn't block the other threads, but without knowledge of a particular implementation this is just speculation.

Joni
  • 108,737
  • 14
  • 143
  • 193
3

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).

Scheduling wise, kernel threads are scheduled at the os level by an alarm clock that interrupts the kernel periodically. IMO, when that happens, your JVM thread(potentially multiple) may get to execute it's code, or may spawn another thread (which could cause the os to create another kernel thread or block it depending on the mapping model), or may run it's own particular scheduling algorithm to decide which thread which were previously blocked on the same lock will be context-switched next etc.

stdout
  • 2,471
  • 2
  • 31
  • 40
0

Starting from Java 19, virtual threads will be part of Java(as a preview feature).

Unlike Java classic threads (aka, platform threads, which are just wrappers around OS threads), virtual threads are managed by the JVM. They are super lightweight, take less memory, have less creation overhead, and can make good use of non-blocking libraries, so they don't get to block the actual OS threads on I/O operations. read more about Project Loom