-4

In the below program,

//Producer - IO bound

public class FileCrawler implements Runnable{
   private final BlockingQueue<File> fileQueue;
   private final File root;
   ....
   public void run(){
      try{
          crawl(root); // IO bound
      }catch(InterruptedException e){
          Thread.currentThread().interrupt();
      }
   }

   private void crawl(File root) throws InterruptedException{
      File[] entries = root.listFiles(fileFilter);
      ...
      for(File entry: entries){
         fileQueue.put(entry);
      }
   }
}

//Consumer - CPU bound

public class Indexer implements Runnable{
   private final BlockingQueue<File> queue;
   ....
   public void run(){
      try{
          while(true){
             indexFile(queue.take()); // CPU bound
          }
      }catch(InterruptedException e){
         Thread.currentThread().interrupt();
      }
   }  
}

FileCrawler is IO bound runnable task that gets launched on multiple threads which perform crawl(root) IO functionality.

Java thread is internally mapped to native thread(equivalent to pthread_create()). Each pthread is mapped to a different thread in the kernel, and the kernel is responsible for scheduling the threads.

So, each java thread is visible to OS. It runs on specific cpu core.

Assume java process is running on OS that follows 1:1 threading model.


A java thread performing IO on a cpu core,

Does producer thread waiting on IO triggers kernel to context switch out the java process and put the java process into a waiting state until the IO is ready to be processed? Not getting chance for other threads(CPU bound) of java process to consume CPU time slice.

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Try to run the program and see whether it's true. – xiaofeng.li Oct 24 '17 at 04:35
  • @xiaofeng.li Before you run the program, we need to understand that this is platform dependent scenario, that affects performance. Please read my query again – overexchange Oct 24 '17 at 04:49
  • 2
    Of course not, that's one of the major points of a 1:1 thread model. This is your Operating Systems course homework in disguise with a very specific question (i.e. the details of the code don't matter, as long as the producer doesn't block on I/O while holding a lock on the queue.) The wiki article you linked even mentions blocking the whole process as a downside of N:1 (user-space threads), as applying only to that, not to 1:1. – Peter Cordes Oct 24 '17 at 04:54
  • @overexchange I'm suggesting you to run the program and find out. Not running it myself. :) – xiaofeng.li Oct 24 '17 at 05:07
  • @xiaofeng.li For producer thread, thread dump shows `java.lang.Thread.State` as `RUNNABLE` on file IO operation. Is it that IO bound thread goes in `WAITING` state on completion of its CPU slice? Am unable to catch this in runtime and I know I can't. Not sure, what do you want me to run? – overexchange Oct 24 '17 at 14:38
  • To verify that IO bound thread doesn't block other threads in your environment. Actually I think you only need two threads, one doing a blocking read on STDIN and the other one doing some loop stuff and print something once in a while and you'll see. Anyway most of the JVM/OS combination will work fine. – xiaofeng.li Oct 24 '17 at 23:16

1 Answers1

0

Java thread is internally mapped to native thread (equivalent to pthread_create()).

What Java threads map to is implementation-dependent.

Each pthread is mapped to a different thread in the kernel

This is just nonsense.

and the kernel is responsible for scheduling the threads.

Correct, if the Java threads are native threads.

So, each java thread is visible to OS.

Correct if ditto.

It runs on specific cpu core.

Not necessarily.

Assume java process is running on OS that follows 1:1 threading model.

A java thread performing IO on a cpu core,

Does producer thread waiting on IO triggers kernel to context switch out the java process and put the java process into a waiting state until the IO is ready to be processed?

No. The process remains runnable if it has other runnable threads.

Not getting chance for other threads(CPU bound) of java process to consume CPU time slice.

No, the other threads can still run if they are runnable.

This is all very confused and rests on a number of incorrect or implementation-specific assumptions.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • In 1:1 threading model, what is nonsense about *user threads* mapping to *kernel threads*? – overexchange Oct 24 '17 at 05:10
  • What's right about it? Where's your evidence? Why would you need all three of a user thread and a native thread and a kernel thread? What *is* a kernel thread? What are you talking about? – user207421 Oct 24 '17 at 09:00
  • [here](https://superuser.com/a/455317/707088) it mentions more about, user thread and kernel thread. Wrt native thread, I was talking from JVM perspective. – overexchange Oct 24 '17 at 12:46
  • [Here](https://stackoverflow.com/a/26325010/3317808), it talks more about user threads and kernel threads – overexchange Oct 24 '17 at 13:11
  • For your point, *the other threads can still run if they are runnable*, are you considering both 1:1 and N:1 threading model? – overexchange Oct 24 '17 at 13:26