How to kill a thread in java? - java

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?

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.

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.

Related

How to kill a thread running in the background - Java?

I have situation where for every hit from a user a thread from a thread pool will be running in the background. So when multiple users hits there will be multiple threads running in the background. Now when one user refreshes their browser I want to kill that thread running the particular user's browser window so that the thread goes back to the thread pool.
Is this possible? How can I do it?
Thanks in advance
Hopefully this helps.
{jstack JAVA_PID}
Gives you list of threads with process id and thread id which are run in the jvm.
The thread id is hexa format. Map this output to top command which is mentioned below.
top JAVA_PID

Stop running thread group in java

I have a processMedia method which takes like 1 mint and multiple threads hits the same method when ever user refresh the page.
So after any of the running thread finish process that media, I want to stop all the threads which running for process that media.
So I need to kind of map all threads with media id and stops them. How can I do it in proper way?

JavaFX: Worker Thread needs JavaFX App Thread to proceed [duplicate]

This question already has an answer here:
JavaFX2: Can I pause a background Task / Service?
(1 answer)
Closed 8 years ago.
A worker thread is used to process a very long-running task. At the middle of task execution, a user interaction is needed to acquire inputs. As the inputs are supplied, the worker thread will resume execution. My problem is, the worker thread needs to be suspended, get inputs (shall we say through a dialog box - I am using the Dialogs API of JDK8u40 which must be facilitated by the JavaFX App Thread), and resume thereafter. The inputs are not supplied at the start due to dependency of certain situations, and inputs might be needed many times.
A typical example is, files are being copied from one directory to another. As the files are copied, a file with the same file name exists in the destination directory, thus a user interaction is needed to rename the file or skip file copying to avoid file name conflict. In this scenario, the worker thread is supposed to execute file copying, and is needed to be suspended to acquire inputs from user (must be facilitated by the JavaFX App Thread) before proceeding. Platform.runlater(() -> {}); can't do this kind of situation, for it would just queued the Runnable object to be ran at some time in the future.
How to facilitate this scenario? I am new to JavaFX concurrency.
You can use the wait notify mechanism as described here: http://www.avajava.com/tutorials/lessons/how-do-i-use-the-wait-and-notify-methods.html.
When an interaction is needed a runnable is launched through ui thread to prompt the user. The worker thread calls wait. The ui thread eventually gets the user input and notifies the working thread which continues his job according to the message it receives. The difference here is that you do not need to create two threads as the ui thread already exists.

Stop the Running Thread in Android?

I am using the thread for login on Server and I want to stop the Thread as the user press back button, I am using stop() and destroy() method and these methods crashing my application, I think these Methods are depreciated that why I am facing this problem. Please Give me the way to stop thread without using stop() and destroy().
Thread.stop() is deprecated since java 1.1 (~17 years ago...). Java of this method explains the reasons in details. This means that you should never call this method. It is still there for backwards compatibility with code written when I was young.
But what to do if you want to "cancel" the operation done in thread? The answer is that you (developer) should care about this yourself. How? It depends on your application. If for example your thread opens i/o stream you can close the stream. If your thread performs series of operations in loop you should check special flag that indicates that thread should exit and update this flag according to needs of your application (in your case when user presses "back" button.
If you still have problem please try to give more details what does your thread do and you will probably get concrete recommendations how to stop it.
For background thread in android try to use service.
I mean you start a service and put a thread in that service.
If you want to stop that service then pressed back button try "Bound" Service. You will get basic idea here.
http://developer.android.com/guide/components/services.html
Only use a thread if you want to do work repeatedly for a long time. I have never needed to start a thread.
You should look at using an AsyncTask.
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
It works by using a Thread from the ThreadPool. AsyncTask's are easy to stop, have a method to override for background tasks and one to override for post task work which is suitable for updating the UI (as long as the task was started by the UI thread).

Threading in Android

I am currently developing Android app, it needs download content from internet. I use thread to do that and then call runOnUiThread method to update GUI.
I placed a refresh menu on it, if user tried to refresh the content, the download thread will be created and started. The problem is that how can I control the thread order, I need to accept the latest request's response and abandon previous thread requests if there were some other requests still running because the request parameters may have been changed by user. Currently I was using a threadId to do this thing, when a thread finished, it will check its threadId, if it was the latest recored one, it then takes control and render the response. My question is that is there any other proper better solution for this?
Do I need to stop threads when user exit the app? I remember that some book said that do not try stop thread manually and wait itself finish is a good practice, is that true? Should I stop them by calling "stop" or "interrupt" method?
I read some documents around threading in Android and found the class HandlerThread, what is it? In what kind of situation I need to use it?
Rather than starting a new thread for every refresh action I would create a single thread for all the background download work that loops and downloads content as lined up in a queue. That ensures that you don't download content concurrently and also saves resources.
In the GUI you simply queue a refresh request whenever the user prompts you to and can abort a running download by calling HttpRequestBase.abort on the http method instance. The background thread should receive and catch a SocketException and move on to the next queued request.
To end the background thread you just have to end its loop. You can use the Looper and Handler classes to help you with all of the above, the HandlerThread class you mentioned is simply a handy class to create a thread that has a Looper.
The problem with interrupting a thread is that it won't break you out of a blocking I/O request and handling an InterruptException correctly can be complicated. So depending on the situation I would say yes, it is better practice to end the thread by returning from its run method.
i discover this week AsyncTask, and i replace Thread by AsyncTask in some place in my program,
You have doc & sample here, really easy to use :
http://developer.android.com/reference/android/os/AsyncTask.html
when i was using thread GUI was lock, and now it's not locked.
And it's possible to cancel a AsyncTask (but i never try)
You can use an IntentService to start your background operations, the service will operate as "work queue processor" and will execute your calls in order.

Categories

Resources