• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bleh...design question about Threads

 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that's right!
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps your checking should be even more fine grained, so it will also terminate the reading of a single file.
 
Timothy Frey
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. Transient is for serializing (or actually NOT serializing). Volatile is for threading.
 
Prakash Subramanian
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic