aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to return a result to the invoking method from another thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to return a result to the invoking method from another thread?" Watch "How to return a result to the invoking method from another thread?" New topic
Author

How to return a result to the invoking method from another thread?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, I am having a swing application which has a button on it saying "Submit". On submit actually I am calling a method say "callMe()". This callMe() method in turn will return some data to the calling() method. This method is responsible for showing a message on the UI saying "Successful".

When I do it as a simple method call, until and unless the callMe() method is finished execution the swing is not responding. So I am creating a new thread on submit and executing the callMe() method in a separate thread. 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.

How can i do this? Thank you all in advance and good day.

PS: I don't want to use the SwingWorker class.


Love all, trust a few, do wrong to none.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

chaitanya karthikk wrote:PS: I don't want to use the SwingWorker class.

Why not? With its doInBackground, done and publish/process methods it can do exactly what you need.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Cole Terry
Ranch Hand

Joined: Nov 23, 2011
Posts: 45
chaitanya karthikk wrote:
How can i do this? Thank you all in advance and good day.

PS: I don't want to use the SwingWorker class.


I think there are two possible options for you if you don't like the SwingWorker/SwingUtilities:

- You would use Thread.join() in the calling method, the thread that executes the calling method will wait for the callMe() thread to ends.
- 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.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Yeah why not SwingWorker? Anyway you can submit work to an ExecutorSerivce and get back a FutureTask, a handle to the result.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Rob Spoor wrote:
chaitanya karthikk wrote:PS: I don't want to use the SwingWorker class.

Why not? With its doInBackground, done and publish/process methods it can do exactly what you need.

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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

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, or
  • you 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.


    luck, db
    There are no new questions, but there may be new answers.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to return a result to the invoking method from another thread?