0

I am building a Java library that needs to perform a periodic check on some url.

This library will be used by users in their own applications.

The library uses a ScheduledExecutorService that performs this check every 3 minutes...

My question is, can this thread created by my library interfere with the application's threads? Is my approach safe?

Federico klez Culloca
  • 22,898
  • 15
  • 55
  • 90
themonkey
  • 105
  • 8
  • I think using it in the library is just like using it in your application. And so it should be safe I guess. – Akash Sep 22 '20 at 06:41
  • It depends. Depends on how you construct the `ScheduledExecutorService`. If you are in a JEE environment you shouldn't be creating your own threads but rather use the JEE server managed `ExecutorService`. So it depends on how and where your library is being used it it safe or not. – M. Deinum Sep 22 '20 at 07:19

1 Answers1

0

Short answer: Maybe.

Problem: When you're not using a own ScheduledExecutorService, then it's possible, that the ScheduledExecutorService of the spring-project is "too small". In other words: The poolsize is not enough.

But when you create a own ScheduledExecutorService in your libary which are used only by you, than you should be safe.

Edit

It's also problematic in Java EE projects, since they don't allow to create threads.
See Java EE specification and multi threading for details.

Thanks @M. Deinum

akop
  • 1,939
  • 1
  • 11
  • 31
  • 1
    It depends on the use-case and environment that the library is being used in. In a JEE environment, you shouldn't be creating your own threads. Which is what happens when you use the `ScheduledExecutorService`, although indirectly. – M. Deinum Sep 22 '20 at 07:20
  • The ScheduledExecutorService would only be calling a URL to populate a variable only used internally by the library. The application using the library would only not "see" that variable. Would that make things any better? Or still not fully safe to use it? – themonkey Sep 22 '20 at 09:53