How to stop a thread in a plugin

Hi,

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?

Thx!

Anyone who has already used Thread in a gradle plugin could help me please?

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.

Thx for your answer!

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.

Hey this could be a solution indeed, but how i get reference to my Thread from stop task, i’m new to groovy/java so i’m not sure how to to this.

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

I think this could be achieved via a build listener (assuming you only want it running for the lifetime of a single build)

class Worker implements Runnable {
   boolean live = true
   void run() {
      while (live) {
         doStuff() 
         Thread.sleep(1000)
      } 
   } 
} 

class MyListener implements BuildListener {
   def executor = Executors.newSingleThreadedThreadPool()
   def worker = new Worker() 
   void buildStarted(Gradle gradle) {
      executor.submit(worker) 
   } 
   void buildFinished(BuildResult result) {
      worker.live = false
      executor.shutdown()
      executor.awaitTermination()
   } 
} 

class MyPlugin implements Plugin {
   void apply(Project project) {
      project.gradle.addListener(new MyListener()) 
   } 
} 

If it was for a single build, your solution would be cool :slight_smile: But:

That’s why I proposed a standalone process that is explicitly started/stopped.

Ah, well… If you want the thread to outlive a gradle build it will have to be a separate jvm (meaning gradle won’t ‘own’ the thread).

As @st_oehme initially suggested, check out running a gradle task in continuous mode. It sounds to me like this will suit your needs

Hey guys continious build might be helpful indeed!
Thx for your answers!