• Post Reply Bookmark Topic Watch Topic
  • New Topic

Background Threads  RSS feed

 
Varghese Paul
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a GUI application. From the GUI app, I can run database queries. During the time the database query is running, my GUI is unresponsive.

I'd like to modify my application so that my GUI is responsive even while the query is executing. I have tried running the database query through a different thread. However, still the GUI remains unresponsive. Only after the query has completed can i perform any other activity.

Swing GUI event are taken care of by the EventDispatcher thread. How do I create a thread that would run asynchronous of the EventDispatcher?

I've gone through a lot of material but they have only mentioned about the background thread in SwingWorker. How does SwingWorker create background threads? Are they using Daemon threads?

The code I have in the run function of the thread would ultimately contain

rs = statement.executeQuery(... query statement...);

So the next doubt i have is, even if i have put this is a query, the remainder of my code would only run once this statement has returned. So even in a background query, wouldn't the whole program execution pause for this statement to return a value? In which case what difference would it make in putting the query execution in a thread?

I guess I ask too many questions at once. Anyways, Thanks in advance
[ December 07, 2006: Message edited by: Varghese Paul ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threading is a big and non-trivial subject, but good clean fun once you're into it. The Sun Tutorial is a good place to start.

In short, you will put the code that does the query into its own class which extends Runnable. Then you can make it run separately:

That will get your code running in another thread and let this thread continue with the next line of code.

When your query finishes you probably want to get the results or at least a completion message back on screen. You can make yet another Runnable that updates the screen and pass it to the SwingUtilities.

So read the tutorial, give it a shot, let us know what you make!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your last question "In which case what difference would it make in putting the query execution in a thread?" warrants its own reply. That's a good thing to ask. Maybe you reall WANT your GUI to be unresponsive until the query comes back. What could the user do while the qeuery is running? If the asnwer turns out to be "nothing useful" or maybe even "something harmful" then maybe threads are not a good choice.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You definately don't want the GUI unresponsive. You may want to keep them from attempting to run another query or otherwise make changes they might lose as soon as the query is done.

But if the query is in the event dispatcher thread that means its completely unresponsive, no repaint, no nothing, including no "Cancel" button for the query.

If you tried to run the query in a different thread and the GUI remained unresponsive. You failed to run it in another thread. All swing worker does is create a new thread and provide a hook to run something in the event dispatcher thread once its done. Its nifty but if you look at the code its pretty straight forward.

There are definately more issues to work out than "run it in another thread", but a long running task in the event dispatching thread will look like a locked app to your users.

Good luck!
 
Varghese Paul
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions guys !!!


I have the situation under control now. It was a goof-up on my part.

I had designed my code to run as a background thread, and it was. But the sequence of events which I had given gave me the impression that it actually wasn't. Let me clarify.

After the user gives his required query and clicks run, I start the thread which executes the query. Next, I have a form which shows me the executing query with a title in a JTable.

The confusion arose because as soon as I pressed the run button the query executes and the form becomes displayed. The form is instantiated only the first time i click on run. Hence, it takes some time for the object to be instantiated and the GUI to bring the form up. During this period, the run button remains depressed. So I assumed the query thread was not running as an independent thread, but under the event dispather thread. I also started assuming that threads blocking on I/O or query executions never yielded to other threads to execute.

I accidentally stumbled upon the solution when I commented out the thread.start() function.

I did go through the SwingWorker code and yes, it's very straight-forward. I was breaking my head literally for the past 2 days to get a solution. Well, like they say - No better way to learn things than to get down and get dirty.

Thanks a lot, guys !!! ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, good point that we never want the GUI totally locked. Always leave a way out.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, see if FoxTrot interests you. It gives a nice way of keeping the readability of code along with achieving responsiveness of Swing applications. Plus, better exception handling.

Ofcourse, there is no such thing as free lunch. This API has its limitations and usefulness of the same completely depends on appropriate usage. Nevertheless, something worthy of a try.
[ December 11, 2006: Message edited by: Aditya N Jha ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!