There is a Java application with a large number of threads (now usually several dozen, in the future there may be up to several hundred or more) that are executed for a long time (more than 10 minutes at least).

Each stream makes changes to its assigned CSV file string (primitive database). Naturally, the work of streams with the file is synchronized.

The application can be closed at any time by the user, and it is necessary at this moment to change the value of one parameter (which is also used during operation in each stream) in the CSV file, in each row.

I tried to figure it out myself, just when closing the application in a loop, looping through all the lines and putting down the desired value of the parameter. In code, it looks like this (I work in Netbeans IDE)

private void formWindowClosing(java.awt.event.WindowEvent evt) { try { DataBase_arr = CSV.GetCSV(fileName); //Обновили БД в массиве } catch (IOException ex) { Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex); } int db_length = DataBase_arr.length - 1; int DB_str = 0; while (DB_str < db_length) { DataBase_arr[DB_str][9] = "0"; CSV.SaveCSV(DataBase_arr, fileName); try { Thread.sleep(1); } catch (InterruptedException ex) { Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex); } DB_str++; } } 

But after closing the application, there are always a few lines in which the necessary changes have not been made. I think this is due to the fact that some additional threads are terminated later than the main one, in which changes are made when the application is closed, which is absolutely normal behavior for them.

But how best to solve this problem so that the solution is correct?

1) When you close an application, create a new thread in which to make changes to the file, and hope that it will be completed last? Honestly, some kind of perversion, and may work unstable if there is a situation that writing to the file will be blocked by another thread (not sure about this).

2) In the stream itself, is it checked whether it is interrupted (because when the application is closed, the stream is interrupted?), And if it is interrupted, make the necessary changes to the file, and then exit it? It will be right? How to do it?

3) Your ideas?

PS Please do not suggest using the thread pool (the question is not about this, and it is not suitable for solving my tasks.), As well as refusing to work with the CSV file (planned for the future, but for now there is no possibility to refuse it).

Just do not want to once again touch the priorities of threads.

  • It will correctly monitor the state of the streams upon exit and wait for them to complete. - ArchDemon Sep.
  • four
    Judging by the code, your csv-file is read into memory at once completely. So read it at the start of the application once. And work with the data in memory, without referring to the file anymore - it will be faster. For some reason you write an array to a file as many times as there are lines in it - the meaning of this is not clear. - Alexander Petrov

0