There is a multithreaded code that works correctly using system threading:
// Main thread Thread producer = new Thread(resultDAO); Thread consumer = new Thread(resultsLoader); producer.start(); // start first added thread consumer.start(); // start second added thread try { consumer.join(); } catch (InterruptedException e) { e.printStackTrace(); }
It is necessary to alter this code using java.util.concurrent
, i.e. with software organization multithreading. Posted by:
// Main thread Thread producer = new Thread(resultDAO); Thread consumer = new Thread(resultsLoader); ExecutorService threadExecutor = Executors.newCachedThreadPool(); threadExecutor.execute(producer); threadExecutor.execute(consumer); threadExecutor.shutdown(); while (!threadExecutor.isShutdown()) { System.out.println("Main THREAD WAIT"); try { threadExecutor.wait(100); } catch (InterruptedException e) {
but the main thread continues to work when the subflows that have not completed their work continue, and then it conflicts with access to the database with the consumer
. What am I doing wrong? How do java.util.concurrent
make an analogue join()
?