views:

28

answers:

1

Hi, I write to understand if and how I could do the following thing.

I have a WebApp for management, in which I should add a button (or something similar) to start and stop a Java thread (this thread polls on DB, send email and so on). The aim is to enable users to manage the life of this thread, deciding whether to keep it alive for a time, alive forever, stop, and so on ....

The logic is:

  1. if you click "start", the thread runs, if there isn't already an instance running;
  2. if you click "stop", stops it.

The running instance must be unique so that different users (group admin) does not initiate multiple threads.

Question 1. This is what should be done?
Question 2. I do not understand if it is possible to retrieve an instance of a thread already running.
Question 3. Is possible to give a "name" to a thread?

I searched on Google but have not found anything, maybe because I looked bad.

Thanks a lot.

A: 

The first question I can't answer because it sounds more like a statement ;)

The second one is: Well, unless you abandon the reference to the thread instance (i.e. by writing new Thread().start();) you will be able to invoke methods on that thread instance at any given time. IF you abandon the reference (which you shouldn't) then you can try to find it in the current thread's ThreadGroup (Thread.currentThread().getThreadGroup()).

The answer to your third question: Yes, you can name a thread. Thread namedThread = new Thread("myPetThread");

sicn