Use same thread executor#106
Open
markscottwright wants to merge 7 commits into
Open
Conversation
added 7 commits
May 4, 2021 21:23
adal4j's AuthenticationContext requires an Executor service on which to perform authentication calls. However, this library makes no use of that possible concurrency, so there is no reason to create an expensive new OS level thread for each ADALClientWrapper instance. It also had been cleaning up the ExecutorService in a `finalize` call, likely thinking that Java's `finalize` works the way it does in C#. In Java, however, `finalize` is less useful (and is deprecated in Java 9), and as written the code would hold on to the JVM, preventing exit, until that `finalize` was called (generally two GC sessions later, an indeterminate amount of time.) I suspect the code would leak OS threads as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current code is performing authentication calls on
ExecutorServicethat it then cleans up in afinalizemethod. I suspect this is how the original C# code does it. However,finalizedoesn't work in java the way it does in C# and as written the code will prevent the JVM from exiting unless at least two GC calls have occurred since an ADALClientWrapper has been constructed (create a simplemainthat does nothing but make an IntuneClient call to demonstrate this - the program will hang on exit). It also will keep allocating new OS threads between GC calls.The library as written makes no use of the possible concurrency offered by adal4j's
ExecutorService-based design, so I replaced the currentExecutorServicewith a "run on the current thread" version. It should be more efficient, will not hold onto the JVM and will not allocate unnecessary OS threads.