wood burning stoves*
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
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: 8710
    
    6

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: 8710
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running a method in a thread
 
Similar Threads
How to make sense of my binary data
Thread creation location
UI troubles
Updation of ProgressBar??
Event Dispatching