I’m using a Thread in a custom gradle plugin (to watch changes from directories), thread is created when plugin apply method is called.
Since i’m new to java/groovy/gradle, i’m a bit concern about that thread keeping in memory and being recreated each time i refresh my gradle build or restart my IDE.
In there any good practises to create/kill a Thread in a gradle plugin?
Since your use case is watching for changes: Are you aware of Gradle’s continuous mode that gives you file watching out of the box?
If you still want to use your own Thread, you probably shouldn’t start it in the apply method and rather as part of a task. Your Thread probably doesn’t need to run when the user just does a gradle clean ;).
You should add a timer that stops the Thread after a while. As for “not starting multiple times”: Making it a singleton would be a simple solution.
My problem is i don’t want to run a build when there is file modification from my folders, i just want to generate a file content from those changes and i’m not sure continuous build is what i need. Furthermore i can’t stop my Thread otherwise once stopped my file content will stop being generated if no tasks are executed for a while.
Actually this works fine but i know a Thread is created each time i refresh build or execute a task and i can’t figure out how to know if my Thread is already running or not, i’m not even sure this is possible.
How about adding an explicit start/stop task and making your file watcher a standalone process?. Much like you would start/stop other tools like web servers.
The usual pattern is to have the external process listen for a stop signal on a pre-defined port. You might want to get inspiration from projects like the Jetty server