wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Bleh...design question about Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Bleh...design question about Threads" Watch "Bleh...design question about Threads" New topic
Author

Bleh...design question about Threads

Eric Chang
Ranch Hand

Joined: Jan 27, 2004
Posts: 113
So I'm designing a fairly simple Java application that will perform "processing" on a group of files. Processing the files involves reading in the contents of the files into an ArrayList (just Strings) and once all the files have been read into the ArrayList, the contents of the ArrayList are then sent across a TCP/IP connection.
So the problem I have now is that there is a Stop button on the application which will stop the processing. Now, this may occur during the reading in of the files, or while sending the contents over the connection to another application. The thing is, I'm fairly weak in threading...but I suppose what I would need to do is have both the reading in of the files be a thread and also sending the contents over the connection as a thread (or both in the same thread since they run one at a time)...and then would call a Stop method of some sort when the Stop button is selected.
Does this sound right? Am I way off base here? It's quite embarrassing that I don't know this right off the bat, but it's been a few years since I last coded with Java, so my memory must be failing me.
Thanks!
[ May 18, 2008: Message edited by: Eric Chang ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yeah. Any long-running work kicked off from a GUI, you want to in a thread you create, anyway -- the GUI will freeze if you do your loading and sending right on the GUI thread. So what you do is have a boolean member variable in an object someplace. Mark it "transient", and have your file-reading/sending loops keep checking that variable. As long as the variable is "true", keep processing.

Then you just make sure the button event handler has access to that same object, and a way to set the variable to "false" -- for example, that "stop()" method, which could just look like

public void stop() { theVariable = false; }

then as soon as your processing loop checks that variable again, it aborts.

Easy, right?


[Jess in Action][AskingGoodQuestions]
Eric Chang
Ranch Hand

Joined: Jan 27, 2004
Posts: 113
Awesome, that's what I kind of figured (since the Stop method is deprecated and all).
So really, everytime I finish processing a certain bit of the file processing (ie: after reading in a file or after a single record has been sent over the connection), I should do a check on the variable to see if the Stop button has been pressed right?
Sounds easy to me! Of course, that's before I actually implement it.
Thanks for the help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yep, that's right!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Perhaps your checking should be even more fine grained, so it will also terminate the reading of a single file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Timothy Frey
Ranch Hand

Joined: Jul 22, 2006
Posts: 56
Originally posted by Ernest Friedman-Hill:
...have a boolean member variable in an object someplace. Mark it "transient", and ...


Shouldn't he mark it as "volatile" and not "transient," or am I completely misunderstanding the OP's problem?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

You're right. Transient is for serializing (or actually NOT serializing). Volatile is for threading.
Prakash Subramanian
Ranch Hand

Joined: Feb 03, 2005
Posts: 32
You might also want to look at articles on Swing and Multi-threading, specifically the usage of the method SwingUtilities.invokeLater() and also topics on Swing worker threads. Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bleh...design question about Threads
 
Similar Threads
interrupting a Socket.accept()
Long running request returns no HTTP response
testing an ant run application
Design 01
Handling Uploaded Files