Skip to content

Use same thread executor#106

Open
markscottwright wants to merge 7 commits into
microsoft:masterfrom
markscottwright:use-same-thread-executor
Open

Use same thread executor#106
markscottwright wants to merge 7 commits into
microsoft:masterfrom
markscottwright:use-same-thread-executor

Conversation

@markscottwright
Copy link
Copy Markdown

@markscottwright markscottwright commented May 6, 2021

The current code is performing authentication calls on ExecutorService that it then cleans up in a finalize method. I suspect this is how the original C# code does it. However, finalize doesn'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 simple main that 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 current ExecutorService with 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.

Mark Wright 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant