chaitanya karthikk wrote:But i am unable to find a way to notify the calling() method that the execution has finished and you can place the message on the UI.
PS: I don't want to use the SwingWorker class.
Anything you roll yourself will essentially mimic the solution already implemented in SwingWorker.
Anyways. You would 'place the message on the UI' in a method call wrapped in a SwingUtilities#invokeLater(...) at the end of the run() method of the background Thread.
Cole Terry wrote:- You would use Thread.join() in the calling method, the thread that executes the calling method will wait for the callMe() thread to ends.
And how would that
not freeze the GUI? The whole purpose of using a background thread would be lost.
Cole Terry wrote:- You would implement the event model (publisher/consumer): upon finishing its tasks, the callMe() method fire an event which is listened by a listener. The listener then will show the message on the GUI.
But make sure the listener code executes on the EDT.
chaitanya karthikk wrote:I don't want to use it because since it is a swing application we have SwingWorker class, but what if I face the same problem somewhere else. Other reason is I want to know how to do without SwingWorker class.
There's no one-size-fits-all. Swing method calls (and constructors/field assignment statements) must be done on the EDT. A non-Swing application may have its own special Thread (like
Java FX 2.0's Application thread) or may even be multithreaded. The solution for such cases wouldn't be the same as the one you develop for a Swing application.
It appears to me that either
this is some homework exercise where the use of SwingWorker has been specifically banned, oryou have found SwingWorker difficult to comprehend, and are ploughing ahead under the mistaken assumption that home-grown code with equivalent functionality will be simpler. It won't.
Use SwingWorker in a Swing application. Use javafx.concurrent.Worker in a JavaFX application. Rolling your own code to mimic their functionality is like tapping the cradle switch of a telephone to dial a number.