简体   繁体   中英

How much can I rely on Java's garbage collection (Groovy)?

I have a statusbar in a Swing application that continuously prints status. All objects and classes within the application will be using this setStatus method to paint the status bar

public def setStatus(statusText){       
        Thread.start {
             ljaStatusBarButton.setText("Status : $statusText . . . .")
             sleep(3000)
             ljaStatusBarButton.setText("Status : Waiting for user action . . . .")
                     interrupt() // or stop() ?  
        }
    }

The status bar will display the status for 3 sec and revert back to a status saying waiting for user action.

This does work correctly but my concern is that the above method will be call multiple times from the UI which also means a new Thread object will be created everytime a status is set. Keeping this is mind, I've explicitly added a interrupt() at the end as I want to indicate the compiler that I am not in need of this thread anymore. And also, how much can I bet on Java's garbage collection to ensure that the stopped/interrupted threads will be cleaned up soon? Or is there a better workaround for this multiple thread objects issue?

You don't need the interrupt() .

In Java, a thread ends when the run() method completes (either normally by returning, or abnormally by throwing an exception).

This means that as soon as your second setText() call is executed, the Thread will stop running. And since nothing else references the Thread object that was created, it will sooner or later be garbage collected.

Your code is incorrect as you are using Swing outside of the EDT

As you are using groovy already, why not use the SwingBuilder helpers that were added to make this sort of thing easier?

import groovy.swing.SwingBuilder

...

public def setStatus( statusText ) {
  // If we are not already on the EDT, static SwingBuilder.build(Closure) will do that for us.
  // In the case of an event handler like the actionPerformed for the button, then naturally
  // we're on the EDT already and the building will continue immediately.
  SwingBuilder.build {

    // doOutside will run the following code in our own thread
    doOutside {

      // Then change the swing components back on the EDT
      edt {
        ljaStatusBarButton.text = "Status : $statusText . . . ."
      }

      // So this sleep is in our own thread again (from doOutside)
      sleep(3000)

      // Then change the swing components back on the EDT
      edt {
        ljaStatusBarButton.text = "Status : Waiting for user action . . . ."
      }
    }
  }
}

Taken in part from the MultiThreading with SwingBuilder example

The Java Garbage collection will free the space your Thread uses at least when it's needed for another Object. You don't have to care about when it happens, if you need the space, you get it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM