Java - IO绑定线程 - 1:1线程模型 [英] Java - IO bound thread - 1:1 threading model

查看:123
本文介绍了Java - IO绑定线程 - 1:1线程模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中,

//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 是IO绑定的可运行任务,它在多个线程上启动,执行 crawl(root) IO功能。


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

Java线程在内部映射到本机线程(相当于 pthread_create())。每个pthread都映射到内核中的不同线程,内核负责调度线程。

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.

因此,每个java线程对OS都是可见的。它运行在特定的cpu核心上。

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

假设java 进程正在运行在操作系统上遵循 1:1 线程模型。

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

在cpu核心上执行IO的java线程,

A java thread performing IO on a cpu core,

生产者线程是否在等待on IO触发内核到上下文切换java 进程并将java进程置于等待状态直到IO准备好处理?没有机会让java进程的其他线程(CPU绑定)消耗CPU时间片。

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.

推荐答案


Java线程在内部映射到本机线程(相当于 pthread_create())。

Java线程映射到的是依赖于实现的。

What Java threads map to is implementation-dependent.


每个pthread都映射到内核中的不同线程

Each pthread is mapped to a different thread in the kernel

这只是废话。


且内核是负责安排线程。

and the kernel is responsible for scheduling the threads.

如果Java线程是本机线程,则更正。

Correct, if the Java threads are native threads.


因此,每个java线程对操作系统都是可见的。

So, each java thread is visible to OS.

如果同上,则更正。


它在特定的cpu核心上运行。

It runs on specific cpu core.

不一定。


假设java进程s在操作系统上运行,该操作系统遵循1:1线程模型。

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

在cpu核心上执行IO的java线程,

A java thread performing IO on a cpu core,

IO上的生产者线程是否会触发内核上下文切换java进程并将java进程置于等待状态,直到IO准备好被处理?

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.


没有机会让java进程的其他线程(CPU绑定)消耗CPU时间切片。

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.

这篇关于Java - IO绑定线程 - 1:1线程模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆