简体   繁体   中英

Java multiple files different threads

I need to work on task in which I am watching a directory using watcher service, now whenever any new files comes in to the directory, I am creating thread with executor service threadpool. I want to know the thread class which I have, contains logic for reading and saving that data into database, Do I need any synchronize block or synchronize method ?

Note: I have am creating separate thread for each file.

Simple answer: if those different threads can cause "data consistency" issues when running in parallel, then you need to synchronize their work.

If there is no problem with what those threads are doing, then you don't need to synchronize them.

The catch here: it is your code, your requirements. You are the person who knows what these threads are doing; and how they manipulate the content of your database.

This is all that can be said here (unless you are simply not sure what your code is doing exactly, then it might be an option for you to enhance your question and show that code of yours that is supposed to run in parallel).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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