multithreading - How to run threads in parallel on priority basis in Java? -
i want read csv files folder , persists data database. can achieve in single thread model want implement using multithreading. have devided tasks 4 threads:
thread 1: files folder , if found suitable file pick , put in temp folder , remove old files.
thread 2: read file temp folder , create list of data files. create output , error data based on validations.
thread 3 [low priority]: check output , error data , write output , error log files.
thread 4: checks data in list , if found data, inserts them database.
for threads, want put check if there not input data thread goes wait , notify others once completed work controll should handover other thread. , these threads should execute in countinue bases.
as not familier multithreading concept, please suggest me how achieve scenario.
you have problem , want use multi-threading solve it. have 2 problems. :)
first of have @ way decompose problem can done in parallel. way have decomposed problem right inherently sequential. there no advantage multi-threading when decompose way because through put limited slowest step in process.
a better way decompose problem break job tasks repeatedly executed. slowest, parallel part reading , uploading files. can each file in parallel. allows leverage javas high level concurrency objects safe world of pain.
thread 1: watch input folder files. when new file appears, create task. task submitted task executor. executor execute task on own thread using 1 of available threads.
task: given file task reads, parses , validate data in file. when data valid write database , delete/move original file.
thread 2: watch task executor completed tasks. when task complete, read information , write report or log error if went wrong.
thread 3: since have tasks going on in parallel need monitor user requests stop service gracefully. don't want stop service halfway through writing report.
logging: logging separate concern has nothing (much) threading. should use logging framework this. frameworks can handle logging multiple threads.
finally note problem doesn't sound problem benefit form parallel processing. reading files, , uploading them database both io operations , io operations glacially slow compared other computations. they're bottle neck parallel processes. can't read files faster disk can provide them , can't upload faster connection can handle. have ask if marginally improved speed worth added complexity.
Comments
Post a Comment