This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes Running a method in a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Running a method in a thread" Watch "Running a method in a thread" New topic
Author

Running a method in a thread

Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Hello,
As a part of my application I have a I/O method that stores all the positions of the start of the file lines in an ArrayList. It gets called when the user selects the file from a file chooser through the gui. The problem is that when the method is called a part of my gui disappears. I've read alot about threads and the SwingWorker but still not clear on how to implement these into my app. Would someone show me how to get this method to run in a new thread? I'm hoping this will keep the gui from flickering or disappearing when this method is executing. Thank you. Here's the code...
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8874
    
    8

You are correct in your assumption. Using the Swing event thread (i.e. the one that notifies your class of GUI events) to run application logic will cause the GUI to be unresponsive until the application logic returns. You have a classic producer-consumer problem here: some action produces a File in need of processing, and your method above is the consumer. It will take a bit more than just changing the one method above to implement a multi-threaded solution, but it isn't too complicated. Check out the Java Tutorial: Threads. It discusses the various problems that can creep up when using threads and has a simple example of a producer-consumer program.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Thank you Joe. I changed a couple of things. First I made the whole class that owns this method "class Processor" to extend Thread. I put the loadFile method in the run() like this...

This is the method call in the main class...

The openFile() method is called from a mouse listener ..

This seemed to help some but there is still a small disappearance of the gui when I load the file. Should I do the thread work in the mouse listener? Thanks for the help and patience.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8874
    
    8

A couple of things:

Ignore exceptions at your peril. You should either attempt to rectify the situation or provide feedback to the user in exceptional cases. At the very least, use e.printStackTrace() to get a stack dump. As you have them now you will never know if an exception occurs, and your user may just figure the program's is unusable.
Second, it's bad form to create a thread and let it run through once. Threads aren't free. They require processor time and native resources to allocate. Creating a new thread for each loadFile() call probably costs you more than your previous single-threaded design. Put a while(true) loop in your run() method and use wait() and notifyAll() to control it (explained in the Java Tutorial).

Should I do the thread work in the mouse listener?

I doubt that the overhead of invoking openFile() is causing the delay.
 
jQuery in Action, 2nd edition
 
subject: Running a method in a thread