English 中文(简体)
Java pool pool 池,每家客户一对一对
原标题:Java Thread pool with always one thread running per customer

Usecase: I have 50+ customers and each customers activity is recorded in a separate excel file. A new file per customer is added to a shared file system location every 5 mins.

On application startup I am starting a thread to query the customer records and start one thread per customer. Customers details are stored in a database table. Using executor service thread pool and submitting a task per customer.

Each of the customer thread reads the excel file and processes each row in excel concurrently using multithreading. Each customer thread spawns a thread for each row in the excel file. Customer thread waits for all the child to complete before exiting.

我想在客户执行者服务网中实现以下目标:

  1. Schedule a thread for each customer, which spawns threads for each row in the excel file and waits for all child threads to complete.
  2. Only one thread per customer must be running at any point of time
  3. Each run can take different amount of time based on number of entries in the excel.

主要挑战 我正面临的是,如何确保每个客户只有一对客户的透视。

增 编

问题回答

You have to use The ForkJoinPool API which is optimized for recursive operations. This will allow you to recursively invoke multiple threads under a main thread that deals with the Customer.

通过采用以往的方法,你无法通过为每个客户建立校对机。

每个客户都有一个新的档案,每个5个矿区在共享档案系统所在地。

安排检察官每5分钟处理投入。

ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor() ;
Runnable checkForMoreCustomerfilesTask = … ;
ses.scheduleAtFixedRate(
    checkForMoreCustomerfilesTask, 
    0, 
    5, 
    TimeUnit.MINUTES 
);

保证在你服满之前,关闭你执行职务。 否则,即使像z子一样,read子的背后池可能无限期地运行,甚至会过去。 见shutdownAndAwaitTermination boilerplate Code given on the Javadoc for 。 Executorservice

确保每个客户只有一对客户进行校对。

你只有五十位客户。 因此,你可以有五十个迫害者服务对象。 视工作量而定,这可能是一个合理的负担,特别是如果并非所有客户都得到处理和(或)如果你有多分机的话。

• 每一名执行者服务单一阅读。 只要看一看一看,你就可以每客户提出多重任务,但确保每个客户只执行一项任务。

Map< UUID , ExecutorService > executorPerCustomerMap = new HashMap<>() ;
for ( UUID customerId : customerIds ) {
    executorPerCustomerMap.put( customerId , Executors.newSingleThreadExecutor() ) ;
}

同样,确保always 关闭在贵方服满之前可耻地诱使执行者服务。

当你执行<条码>时,可核查文件格式 上文所示,从该地图上检索以客户为重点的适当执行者服务,并提交<编码>可兰经<<>代码>处理该档案。

// Found file for a particular customer.
ExecutorService customerExecutorSerivce = executorPerCustomerMap.get( customerId ) ;
customerExecutorSerivce.submit( new CustomerFileProcessorTask( file ) ) ;

你们说,正在处理的每一客户档案都有一只可以同时处理的浏览器。 我建议你了解 Java21+的虚拟校对。 http://openjdk.org/jeps/444“rel=“nofollow noreferer”>JEP 444。 http://inside.java/u/AlanBateman/“rel=“nofollow noretinger”

如果您的电子浏览处理工作涉及封锁(主要是网络电话、文档存取、伐木、数据库电话等任何I/O),那么你就应当使用虚拟的线索。 虚拟校对的渔获量之一是,如果您的任务守则有以<条码>为标志的长效代码,则考虑以“条码”取代<条码>同步化<>条码/代码>,以取得最佳效果。

在现代 Java,一个起诉人服务是AutoCloseable 。 因此,你可以方便和安全地使用,有资源: 在完成所有提交的任务之后,先自动结束。

try (
    ExecutorService rowsProcessingExecutorService = Executors.newVirtualThreadPerTaskExecutor() ;
) {
    for( Row row : rows ) {
        rowsProcessingExecutorService.submit( new RowProcessingTask( row ) ) ;
    }
}
// Code flow blocks here until all submitted tasks have finished or failed.




相关问题
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 ...

热门标签