简体   繁体   English

Java多个文件不同的线程

[英]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). 这就是在这里可以说的全部内容(除非您只是不确定代码到底在做什么,否则它可能是您增强问题并显示应该并行运行的代码的一种选择)。

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

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