You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some internal changes and fixes in the FSUIPC class. For example, it was not possible to start processing requests again after disconnecting/connecting again. Some cleanup was not being done.
logger.log(Level.FINER, "Started new task to wait to connection to sim via FSUIPC. Required sim version is: {0} and repeat period is: {1} seoonds.", newObject[]{getFSVersion(simVersion), repeatPeriod});
//this one, we will let finish if already running - the false parameter
692
728
continualRequestProcessThread.cancel(false);
693
729
//wait for the task to complete
694
730
while (!continualRequestProcessThread.isDone()) {
695
-
Thread.sleep(50);
731
+
try {
732
+
/* It is said that using Thread.sleep in the loop is bad design pattern, but here, I do not know about
733
+
better solution for waiting for the thread to be completly finished.
734
+
*/
735
+
Thread.sleep(50);
736
+
} catch (InterruptedExceptionex) {
737
+
//just continue on processing
738
+
logger.log(Level.WARNING, "Interrupted while waiting for continual request processing thread to finish!", ex);
739
+
}
696
740
}
741
+
continualRequestProcessThread = null; //important so that the condition above works next time
742
+
//clear array of requests
743
+
arContinualRequests.clear(); //added as anothe call to start request processing in one session would add request (double them and so on)
697
744
logger.finer("Thread for FSUIPC continual requests processing was canceled!");
698
745
}
699
746
} catch (Exceptionex) {
700
747
logger.log(Level.SEVERE, "Failed to cancel thread for FSUIPC continual requests processing!", ex);
701
748
returnfalse;
702
-
}
749
+
}
703
750
returntrue;
704
751
}
705
752
@@ -710,19 +757,57 @@ public void disconnect() {
710
757
logger.info("Called disconnect! Will close FSUIPC connection and release resoures.");
711
758
setConnected(false);
712
759
713
-
//cancel runnig threads if any
714
-
cancelWaitForConnectionTask();
760
+
//cancel runnig threads waiting for FSUIPC connection if any
761
+
if (scheduledESForWfCTask != null) {
762
+
try {
763
+
scheduledESForWfCTask.shutdown();
764
+
while (!scheduledESForWfCTask.isShutdown()) {
765
+
//wait for tasks to shutdown
766
+
try {
767
+
/* It is said that using Thread.sleep in the loop is bad design pattern, but here, I do not know about
768
+
better solution for waiting for the executor to be completly shutdown. We could call awaitTermination,
769
+
but it has tim limit and if tasks does not finish within limit, than we still have to take another action,
770
+
so may not be sure the executor finished all and was shutdown
771
+
*/
772
+
Thread.sleep(50);
773
+
} catch (InterruptedExceptionex) {
774
+
//just continue on processing
775
+
logger.log(Level.WARNING, "Interrupted while waiting for \"Wait for FSUIPC connection\" task executor to shutdown!", ex);
776
+
}
777
+
}
778
+
scheduledESForWfCTask = null;
779
+
waitForConnectionThread = null;
780
+
logger.finer("The \"Wait for FSUIPC connection\" task executor is shutdown. Thread waiting for connection is terminated.");
781
+
} catch (Exceptionex) {
782
+
logger.log(Level.SEVERE, "Failed to terminate the \"Wait for FSUIPC connection\" task executor! The waiting thread might still be running!", ex);
783
+
}
784
+
}
715
785
716
-
//terminate all running threads
717
-
try {
718
-
scheduledExecutorService.shutdown();
719
-
while (!scheduledExecutorService.isShutdown()) {
720
-
//wait for tasks to shutdown
721
-
Thread.sleep(50);
786
+
//terminate all running continual request processing threads
787
+
if (scheduledESForCRPTask != null) {
788
+
try {
789
+
scheduledESForCRPTask.shutdown();
790
+
while (!scheduledESForCRPTask.isShutdown()) {
791
+
//wait for tasks to shutdown
792
+
try {
793
+
/* It is said that using Thread.sleep in the loop is bad design pattern, but here, I do not know about
794
+
better solution for waiting for the executor to be completly shutdown. We could call awaitTermination,
795
+
but it has tim limit and if tasks does not finish within limit, than we still have to take another action,
796
+
so may not be sure the executor finished all and was shutdown
797
+
*/
798
+
Thread.sleep(50);
799
+
} catch (InterruptedExceptionex) {
800
+
//just continue on processing
801
+
logger.log(Level.WARNING, "Interrupted while waiting for \"Continual requests processing\" scheduled task executor to shutdown!", ex);
802
+
}
803
+
}
804
+
scheduledESForCRPTask = null;
805
+
continualRequestProcessThread = null;
806
+
arContinualRequests.clear(); //added as another call to start request processing in one session would add request (double them and so on)
807
+
logger.finer("The \"Continual requests processing\" task executor is shutdown. Thread performing continual request processing is terminated.");
808
+
} catch (Exceptionex) {
809
+
logger.log(Level.SEVERE, "Failed to terminate the \"Continual requests processing\" executor service! The thread performing continual request processing might still be running!", ex);
722
810
}
723
-
logger.finer("All scheduled threads stopped and executor service terminated.");
724
-
} catch (Exceptionex) {
725
-
logger.log(Level.SEVERE, "Failed to terminate all scheduled threads and to stop executor service!", ex);
726
811
}
727
812
728
813
//close FSUIPC connection. We do it as last command, after cancelling all processing, as doing it as firts, may
@@ -819,15 +904,15 @@ public String getVersion() {
checkLastResult(); //check whether we are still connected
@@ -1003,6 +1088,9 @@ public int processRequestsOnce() {
1003
1088
* functions. If processing is completed without errors, the "one time"
1004
1089
* requests array will be cleared.
1005
1090
*
1091
+
* <strong>WARNING:</strong> This function will try to stop any currently running processing thread (if cancelRunning is true). This might take some time - thus may
1092
+
* block the current thread! Should not be called from main EDT thread.
1093
+
*
1006
1094
* @param repeatPeriod How often to process the requests. Milliseconds.
1007
1095
* @param cancelRunning Whether to cancel the task if currently running and
1008
1096
* start a new one.
@@ -1030,8 +1118,18 @@ public int processRequests(int repeatPeriod, boolean cancelRunning) {
1030
1118
}
1031
1119
1032
1120
try {
1121
+
//init task executor if not initialized yet
1122
+
if (scheduledESForCRPTask == null) {
1123
+
//Creating thread pool with only one thread will make sure that no more than one thread will be processing requests at one time
1124
+
//thus, listener functions that are called from processing thread will have to be completed before the thread can run next time
1125
+
//Hope this will prevent data incosistency that might would occur when the executor would run the same processing code again
1126
+
//before the completion of previous code (which could happen with more than one thread in pool). This would cause data issues where
1127
+
//data in instances that are read by FSUIPC would be overwritten before listener has time to process them, as FSUIPC lib writes the changes
0 commit comments