How to organize data reading (one object each) from a file by one stream and transfer the second stream through the buffer object to the second stream, which will put the data in the database. The structure of the organization of flows interests. Which objects from the above java.util.concurrent package should be used, how to organize synchronized access of these streams to the buffer object that has the readBuffer and writeBuffer methods.

    2 answers 2

    I would make two threads of 2 types.

    Thread that writes to the database

    public class WriterThread extends Thread { private ConcurrentLinkedQueue queue<DbObject> = new ConcurrentLinkedQueue queue<DbObject>(); public void run() { while(true) { DbObject obj = queue.peek(); if(obj!=null) { boolean result = writeToDb(obj); if(result) queue.poll(); } else { // в буфере пусто, ждем Thread.sleep(3000); } } } public void add(DbObject obj) { queue.add(obj);} } 

    A thread that reads from a file (there may be several to read from different files). It just reads objects from a file and calls add (DbObject)

    • The answer is good, but the problem has limitations on the use of collections and sleep () as a buffer; - rvm

    without any collections and additional "buffers":

      // поток который будет непрерывно считывать поток данных из файла class FileReader extends Thread { private DbWriter listener = null; public FileReader(DbWriter writer) { setWriter(writer); } protected DbWriter getWriter() { return listener; } private void setWriter(DbWriter listener) { this.listener = listener; } public void read() { // считываем данные из файла в какую-то переменную Object data = null; getWriter().handleData(data); } } // класс который отвечает за сохранение данных в СУБД abstract class DbWriter { // в принцыпе если время вставки соизмеримо по времени с временем чтения или меньше // то данный лок не нужен. с другой стороны если важен строгий порядок вставки данных // от этот механизм синхронизации лучше оставить private Object dataLock = new Object(); public boolean handleData(Object data) { synchronized (dataLock) { return handleDataImpl(data); } } protected abstract boolean handleDataImpl(Object data); } 

    “Object data” acts as a buffer, which should be of the type you need in order to store a separate portion of data (for example, 1 line read from a file)

    ps if the use of a concurrent package is a necessary condition, you can use it in order to synchronize the calls to the " DbWriter.handleData " method enter the code here