I have a requirement to launch a single instance of a task to run in the background in a continous loop. i.e I only want it to stop when I tell it to. The task will continually poll a message queue for data and process it when found. I guess therefore I don't want a timer task. I need to be able to check that the task is not already running in another thread and hence prevent a second thread from running. Some static variable flag ? I also need some way of requesting a gracefull shutdown of this single thread. I'm assuming that having only one instance (for want of a better term) of this thread running I will minimise thread safety issues - although that's not why I want only one instance running. Will it be possible for this thread to update a swing gui object (list or table) while it's running so there will be some indication of progress ?
If anyone has done this sort of thing (I'm using java 6) and has a simple example it would be great. Googling comes back with too many hits and it's confusing me !
It sounds like you might want to look at the Java 6 SwingWorker. This class provides methods to safely start a background thread from Swing, to send data and progress notifications back to the Swing thread, and to interrupt the background thread to tell it to finish, and it can only be started once.
(Note that there are other, older versions of SwingWorker that are quite different, but that will come up in search results.)