English 中文(简体)
利用校对池/校对阅读大型轴心文档?
原标题:Using threadpools/threading for reading large txt files?

关于先前的地雷问题,我曾指出:

I have to read several very large txt files and have to either use multiple threads or a single thread to do so depending on user input. Say I have a main method that gets user input, and the user requests a single thread and wants to process 20 txt files for that thread. How would I accomplish this? Note that the below isn t my code or its setup but just what the "idea" is.

例:

int numFiles = 20;
int threads = 1;

 String[] list = new String[20];
 for(int i = 1; i < 21; i++){
   list[i] = "hello" + i + ".txt";//so the list is a hello1.txt, hello2.txt, ...,  hello20.txt
 }

 public void run(){
 //processes txt file
 }

www.un.org/spanish/ecosoc 因此,简言之,我如何用一只透镜来做到这一点? 有20条read?

并建议使用透镜的用户 集合:

When the user specifies how many threads to use, you d configure the pool appropriately, submit the set of file-read jobs, and let the pool sort out the executions. In the Java world, you d use the Executors.newFixedThreadPool factory method, and submit each job as a Callable. Here s an article from IBM on Java thread pooling.

因此,现在我有一种叫做“AndMap(String x)”的方法,它采用的是txt文档名称,而且处理方式,如上。

检察官。

我如何用透镜来利用这一点。 • 集合,以便我上述例子能够做到?

最佳回答

奥基与我一样,因为我需要解释几个问题。

首先,除非有多个磁盘或可能是单一磁盘(SSD),否则建议不使用一个以上的透镜从磁盘读。 关于这个专题的许多问题已经公布,结论是相同的:使用多线透镜从单一机械软盘读,将损害业绩而不是改进业绩。

之所以出现上述情况,是因为磁盘的机械头需要不断寻找下一个位置。 采用多种透镜意味着,如果每个透镜都有机会运行,则将标题引向磁盘的不同部分,从而使磁盘区之间的渗透效率不高。

处理多个档案的公认解决办法是建立一个单一生产者(阅读器)系统——多个消费者(加工线)系统。 理想的机制是,在这种情况下,一个read子作为生产者,把任务放在工人的集合中。

Something like this:

int numFiles = 20;
int threads = 4;

ExecutorService exec = Executors.newFixedThreadPool(threads);

for(int i = 0; i < numFiles; i++){
    String[] fileContents = // read current file;
    exec.submit(new ThreadTask(fileContents));
}

exec.shutdown();
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
...

class ThreadTask implements Runnable {

   private String[] fileContents;

   public ThreadTask(String[] fileContents) {
        this.fileContents = fileContents;
   }

   public void run(){
      //processes txt file
   }
}
问题回答

首先,我想读一下 rel=“nofollow”>tutorial。 我建议阅读整个网站concurrency tutorial,因为它与你一样,是新到的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签