I have jsp page which starts a thread whenever user logins on that page .But what I want is that only a single instance of thread must run i.e. even if user logins for n times on that page only one thread should run.currently whenever user logins a new thread get created which I don't want.For this Either I have to check that one thread is already running or not and if already running then I won't start another, or I can kill the thread which is already running and start a new one.Now ,the problem is How can I do this?I am new to java programming so I don't know my options?
Asked
Active
Viewed 1,176 times
-1
-
possible duplicate of [How do you kill a thread in Java?](http://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java) – Raedwald Jul 16 '14 at 12:11
-
I have already read that answer but was unable to find answer to my question thats why I asked a new question.. – cooljohny Jul 16 '14 at 12:16
-
You need to explain *in your question* why this is not a duplicate of that question. – Raedwald Jul 16 '14 at 12:29
-
same title of the question doesn't mean the question is same.. – cooljohny Jul 16 '14 at 12:32
2 Answers
0
Why not just check oldThread.isAlive()
? and stop spawning a new thread if this returns true.
BTW you should use ExecutorService
for such things.
Also, you cannot actually kill a thread in Java. When a thread's execution finishes, its state will be terminated.

TheLostMind
- 35,966
- 12
- 68
- 104
-
I don't understand how can I do it?I mean whenever user go to that jsp page its a new instance of that page..How can I get the info of that old thread? – cooljohny Jul 16 '14 at 12:12
-
No I don't ..Is there any other solution?How can I get any info about the previous thread so that I can check wheteher its running ,the next time? – cooljohny Jul 16 '14 at 12:20
0
Check out if the user already has a session and if he does, don't start the thread.
In fact if the user already has a session, don't let him go to the login page, go to the front page instead, then he can log out if he wants and log in again.

Kayaman
- 72,141
- 5
- 83
- 121
-
I have a doubt .When the session is lost?I mean session gets removed when the tomcat shutsdown or when the app shuts down? – cooljohny Jul 16 '14 at 12:15
-
1Well if the container is shut down, you don't really need to care about anything anymore. – Kayaman Jul 16 '14 at 12:16
-
-
1Well it's a bit like asking what about when you shut down the computer. Obviously users won't be able to access anything, and any users logged in sure won't be logged in anymore (nor any threads will be running). In fact, shutting down tomcat would be equivalent to shutting down the computer, since the JVM will be shut down. – Kayaman Jul 16 '14 at 12:20
-
What I wanted to ask was when does the session gets removed?I mean if the session gets removed when I close the webapp then that would be of no use to me..for eg the user logins and the thread start running but then user close the app but the thread would still be running in background...and when user logins agains..it will be a nw sesiion and then another thread will start which will create multiple instance of that thread which I don't want.. – cooljohny Jul 16 '14 at 12:26
-
The session will be removed either when you explicitly do it, when the session timeout occurs, or a few other cases (shutting down tomcat, redeploying the webapp..). Looks like you'll need to do a lot of reading before you attempt to program anything. – Kayaman Jul 16 '14 at 12:32
-
In my case the session doesn't get removed if I shutdown and restart again? – cooljohny Jul 16 '14 at 13:34
-
Only if you're using persistent sessions. I doubt that's on by default in tomcat. – Kayaman Jul 16 '14 at 13:35