• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and Classes?  RSS feed

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

I have a program consisting of 4 classes at the moment:

MainProgram.java class
FileProcessor.java class
FileCreator.java class
ServerCommands.java class

The MainProgram class provides an interface where somebody can input a file name and press a button 'Go'. This file name is then passed to the FileProcessor.

The FileProcessor class interprets the data in the user input file and passes some of the interpreted data to the FileCreator class, which stores this data in a new file.

Having done this, the ServerCommands class is then invoked, which issues commands that uses the file to a statistical server that interprets these and produces a statistial output. However, as this takes some time to do and plots the data in a new window that needs to ineractive while open, I created a new thread, called myThread, so that the GUI created by the MainProgram class is still accessible and functional whilst this is going on.

However, on the MainProgram GUI I have a button 'Reset'. What I would like to know is how I can stop the thread from this MainProgram class.

To invoke the FileProcessor class from the MainProgram, having cliked the Go button, the following code was used:




Here is the bottom of the FileCreator class


The ServerCommands looks like the following:



In the MainProgram class, I would like to stop the thread from within the code used to create the reset button which is currently:



Any ideas??

Thanks

Sam
[ July 08, 2007: Message edited by: Sam Bluesman ]
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read up on Thread.interrupt() and interrupted(). Your main program would keep a reference to the thread that's running the ServerCommand and call interrupt when you reset. The server command code would catch an exception and/or check the interrupted status now and then. It's very convenient to do this at the top of a loop, but if you have a long series of sequential steps you insert the checks between steps.

Does that fit the problem?
[ July 08, 2007: Message edited by: Stan James ]
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Thanks for the response. Yes - that sounds right. Iove been reading up on Thread.interrupt() and interrupted() but am having some difficulties in knowing how and where to implement this. Could you give me some guidance? In particular, how can I reference the thread from the MainProgram class?

Thanks very much.
[ July 08, 2007: Message edited by: Sam Bluesman ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance of FileProcessor created by the main class should return the instance of the thread it created. The main class then stashes it in it's own thread variable. When the user clicks the reset button, the main class can then interrupts the thread using the methods suggested by Stan.

HTH,
Travis
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!