Multithreading hotfix

Write thread was prematurely exiting in previous build
This commit is contained in:
Blizzard Finnegan 2023-03-21 08:54:35 -04:00
parent 8be83fa479
commit ac90cd02ae
No known key found for this signature in database
GPG key ID: DE547EDF547DDA49

View file

@ -31,8 +31,6 @@ public class Cli
*/ */
private static int iterationCount = 10; private static int iterationCount = 10;
private static boolean endOfCycles = false;
/** /**
* Scanner used for monitoring user input. * Scanner used for monitoring user input.
* This is a global object, so that functions * This is a global object, so that functions
@ -135,7 +133,6 @@ public class Cli
setActiveCameras(); setActiveCameras();
break; break;
case 5: case 5:
endOfCycles = false;
if(!camerasConfigured) if(!camerasConfigured)
{ {
prompt("You have not configured the cameras yet! Are you sure you would like to continue? (y/N): "); prompt("You have not configured the cameras yet! Are you sure you would like to continue? (y/N): ");
@ -652,22 +649,22 @@ public class Cli
final LinkedBlockingQueue<Cycle> dataEntryQueue = new LinkedBlockingQueue<>(); final LinkedBlockingQueue<Cycle> dataEntryQueue = new LinkedBlockingQueue<>();
Thread writeThread = new Thread( () -> { Thread writeThread = new Thread( () -> {
while(dataEntryQueue.size() > 0 && !endOfCycles) ErrorLogging.logError("DEBUG: Starting write thread...");
while(true)
{ {
Cycle cycle = null; Cycle cycle = null;
do try{ cycle = dataEntryQueue.take(); }
{
try{ cycle = dataEntryQueue.poll(Long.MAX_VALUE,TimeUnit.SECONDS); }
catch(Exception e){ ErrorLogging.logError(e); } catch(Exception e){ ErrorLogging.logError(e); }
}
while(cycle == null); if(cycle.getcycleNumber() == -1) break;
DataSaving.writeValues(cycle,serials); DataSaving.writeValues(cycle,serials);
} }
ErrorLogging.logError("DEBUG: Exiting write thread.");
}); });
writeThread.start(); //writeThread.start();
for(int i = 0; i < localIterations; i++) for(int i = 0; i < localIterations; i++)
{ {
@ -724,8 +721,10 @@ public class Cli
} }
while(fail); while(fail);
dataEntryQueue.add(cycle); dataEntryQueue.add(cycle);
if(!writeThread.isAlive())
writeThread.start();
} }
endOfCycles = true; dataEntryQueue.add(new Cycle(-1));
println("======================================="); println("=======================================");
println("Testing complete!"); println("Testing complete!");
} }