已尝试解决此问题,但证明很困难。
我有两个线程,一个是生产者,另一个是消费者。都在不同的班级。两个线程独立运行。生产者正在汇集大约 8-12 个文件夹用于输入。然后它同时翻译所有文件,并将其放置在一个名为“readyToLoad”的共享文件夹中。一旦生产者线程完成,消费者线程现在进入“readyToLoad”文件夹并开始处理翻译后的文档。
现在的问题是,当消费者处理翻译文件时,它不允许生产者将更多翻译文件放入“readyToLoad”文件夹。
我的问题是,如何防止消费者锁定“readyToLoad”文件夹?我该如何处理这种情况?
为长文本道歉,但我觉得这将有助于理解我在哪里遇到问题。谢谢大家的帮助。
更新:添加了消费者代码(在加载过程中执行加载并锁定文件的代码)
public class LoadManager {
protected static final Logger logger = KCLogger.getLogger();
ArrayList<Loader> threads = new ArrayList<Loader>();
KCBLConfig config;
private static final ExecutorService service = Executors.newFixedThreadPool(10);
public LoadManager(KCBLConfig config) {
this.config = config;
}
public void start() throws Exception {
logger.log(Level.INFO, "Starting loading threads.");
try {
TreeMap<String, ConnectionHandler> connectionHandlers = config.getConnectionHandlers();
Iterator i = connectionHandlers.keySet().iterator();
while (i.hasNext()) {
ConnectionHandler connectionHandler = connectionHandlers.get((String) i.next());
for (LoadFolder loadFolder : connectionHandler.getKcXMLFolders()) {
Loader loader = new Loader(loadFolder.getId(), loadFolder, config.getConnectionHandlers());
Thread loaderThread = new Thread(loader);
loaderThread.start();
//service.submit(loader);
// service.shutdown();
//service.awaitTermination(1, TimeUnit.MILLISECONDS);
threads.add(loader);
}
}
} catch (Exception e) {
logger.log(Level.SEVERE, "There was an error starting the loaders. Stopping all loader threads.", e);
this.stop();
throw e;
}
logger.log(Level.INFO, "All loader threads created. There are " + threads.size() + " loader threads");
}