• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
     
    That new kid is a freak. Show him this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic