简体   繁体   English

如何在 Java 中以优先级为基础并行运行线程?

[英]How to run threads in parallel on priority basis in Java?

I want to read csv files from folder and persists their data into database.我想从文件夹中读取 csv 文件并将它们的数据保存到数据库中。 We can achieve this in a single thread model too but I want to implement it using multithreading.我们也可以在单线程模型中实现这一点,但我想使用多线程来实现它。 So I have devided the tasks into 4 threads:所以我把任务分成了 4 个线程:

Thread 1: Look for files from folder and if it found the suitable file then pick it and put in temp folder and remove the old files.线程 1:从文件夹中查找文件,如果找到合适的文件,则选择它并放入临时文件夹并删除旧文件。

Thread 2: Read the file from temp folder and create list of data from files.线程 2:从临时文件夹中读取文件并从文件创建数据列表。 Also create output and error data based on some validations.还根据一些验证创建输出和错误数据。

Thread 3 [Low Priority]: Check for output and error data and write it into output and error log files.线程 3 [低优先级]:检查输出和错误数据并将其写入输出和错误日志文件。

Thread 4: Checks for data in the list and if found data, inserts them into database.线程 4:检查列表中的数据,如果找到数据,则将它们插入数据库。

For all threads, I want to put a check if there is not input data then that thread goes into wait and notify others also once completed work it controll should handover to other thread.对于所有线程,我想检查是否没有输入数据,然后该线程进入等待状态并在完成工作后通知其他人,它控制的工作应该移交给其他线程。 And all these threads should execute in countinue bases.所有这些线程都应该在计数基础上执行。

As I am not much familier with multithreading concept, please suggest me how to achieve this scenario.由于我对多线程概念不太熟悉,请建议我如何实现这种情况。

You have a problem and you want to use multi-threading to solve it.你有一个问题,你想使用多线程来解决它。 Now you have two problems.现在你有两个问题。 :) :)

First of all you have to look at a way to decompose your problem into something that can be done in parallel.首先,您必须寻找一种方法将您的问题分解为可以并行完成的事情。 The way you have decomposed your problem right now is inherently sequential.您现在分解问题的方式本质上是顺序的。 There is no advantage to multi-threading when you decompose it this way because the through put will be limited by the slowest step in the process.以这种方式分解多线程没有任何优势,因为吞吐量将受到进程中最慢步骤的限制。

A better way to decompose your problem would be to break up the job into tasks that are repeatedly executed.分解问题的更好方法是将作业分解为重复执行的任务。 The slowest, but also most easily parallel part is reading and uploading the files.最慢但也是最容易并行的部分是读取和上传文件。 You can do this for each file in parallel.您可以对每个文件并行执行此操作。 This allows you to leverage Javas High Level Concurrency Objects which will safe you a world of pain.这使您可以利用 Java 高级并发对象,这将为您带来痛苦的世界。

Thread 1: Watch input folder for files.线程 1:监视输入文件夹中的文件。 When a new file appears, create a task.当一个新文件出现时,创建一个任务。 This task is submitted to a Task Executor .这个任务被提交给一个Task Executor The executor will then execute the task on its own thread using one of the available threads.然后,执行程序将使用可用线程之一在其自己的线程上执行任务。

Task: For a given file a task reads, parses and validate the data in the file.任务:对于给定的文件,任务读取、解析和验证文件中的数据。 When the data is valid write it to the database and delete/move the original file.当数据有效时将其写入数据库并删除/移动原始文件。

Thread 2: Watch the task executor for completed tasks.线程 2:观察任务执行器是否完成任务。 When a task is complete, read its information and write a report or log an error if something went wrong.任务完成后,读取其信息并在出现问题时编写报告或记录错误。

Thread 3: Since you have tasks going on in parallel you need something to monitor user requests to stop the service gracefully.主题 3:由于您有并行执行的任务,因此您需要一些东西来监视用户请求以正常停止服务。 You don't want to stop the service halfway through writing a report.您不想在编写报告中途停止服务。

Logging: Logging is a separate concern but has nothing (much) to do with threading.日志记录:日志记录是一个单独的问题,但与线程无关(很多)。 You should use a logging framework for this.您应该为此使用日志记录框架。 Most frameworks can handle logging from multiple threads.大多数框架可以处理来自多个线程的日志记录。

Finally note that your problem doesn't sound like a problem that would benefit much form parallel processing.最后请注意,您的问题听起来不像是一个有利于并行处理的问题。 Reading files, and uploading them to a database are both IO operations and IO operations are glacially slow compared to other computations.读取文件并将它们上传到数据库都是 IO 操作,与其他计算相比,IO 操作非常缓慢。 They're also the bottle neck for all parallel processes.它们也是所有并行流程的瓶颈。 You can't read files faster then your disk can provide them and you can't upload faster then your connection can handle.您无法比磁盘提供文件的读取速度更快,也无法比连接可以处理的速度更快地上传文件。 So you have to ask yourself if the marginally improved speed is worth the added complexity.因此,您必须问问自己,略微提高的速度是否值得增加复杂性。

you can use Thread3.setPriority(Thread.MIN_PRIORITY);你可以使用 Thread3.setPriority(Thread.MIN_PRIORITY); priority is int number 1-10 1 is the minimum and 10 is the maximum优先级是整数 1-10 1 是最小值,10 是最大值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM