This what i am trying to do in my application.
The activity has 2 button
1 - for starting thread (start)
2 - for stopping the thread (stop)
I press a button to start a thread and i don't want to kill my thread when press back
everything okay for now.
The problem is when i come back to that activity there is a button to stop that thread
but the thread been lost so i can't stop it and if i press start a second thread run and i got 2 thread running.
My question is can we save that thread in a bundle? If possible how i don't see a method to put a object. If not any good solution that i can do to avoid losing my thread.
Thank you.
You can't save a thread in a Bundle. In general if you need a task to run after you exit the app you should be using a Service.
Related
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
I am programming a small game. I already implemented my logic in one Java project and it is working correctly (Handling inputs with a Scanner in console).
Now, next step is to create a UI and to replace my Scanner inputs by the clicks generated by the user at certain positions.
I do not really know how to do, because I need the UI thread, and thread from the logic. However, I do not really know how to orchestrate the different method calls and how to start the program (Because I might have 2 Main methods).
What I thought was to move the main function in the UI thread, starting the program. Then it handles user clicks on the menu, and start a thread to launch the logic of the game. 1 - Is that correct ?
Because the game is played by turn, I need to loop the following scenario :
- User clicks, it send data to the logic thread (2 - UI should wait, but is that possible ?)
- Logic thread computes a result and update the UI (3 - How ? By returning the data to the UI thread directly ?)
- Logic thread waits. UI thread wait another click, and send it to logic thread, etc...
If I do not reuse the logic thread, the data contained in the logic thread will be lost. So I think I need to keep the logic thread alive
To resume : How to handle thread (and their communication) between UI with my and my logic/data module ?
Thank you !
This question already has answers here:
Android - What happens to Threads when a Activity finishes?
(2 answers)
Closed 6 years ago.
If we start a handler thread / thread in an activity and then the activity is destroyed when we press back button, What happens to the handler thread?
Is it still in running state?
If Yes When the thread execution will stop?
Simply , your Thread is in running state.
It is actually bad practice to keep a thread running after onPause. The reason is that after onPause your application may drop out of memory at any time without your being able to know, therefore you will not be able to clean up after yourself.
The proper way to do it is stopping the thread onPause and recreating it onResume. If you need state you can use Android's built in saveState methods or settings or whichever to keep that.
Your related thread is here and here
The thread won't be destroyed until its job is finished. So make sure all its job is finished before closing an activity. Because the thread may contain any reference of views and it may try to access it after the job is done. HandlerThread can be stopped by calling
thread.quitSafely();
this ensures that all pending messages are processed before the thread stops.
Sorry but, I'm quite a bit confused after reading this.
If let say, I have a single Activity and inside it,
I tried to make an inner Class implementing Thread that has looper and handler.
My question is;
If I finish() my activity. Is that close any Looper / Handler i just created last time?
I'm afraid the Thread is still running background altough the Activity is already closed -> ended.
After finish, you don't have to care about Handlers attached to the Main Thread, because it's Looper (and the Thread itself) is managed by the system, and it will quit when it is necessary.
However if the Handler is attached to a separate Looper that is not managed by the system (for example a Thread with a Looper started by you), it will be there in case you have not stopped the relevant Thread (that has the Looper). This true in general for all Threads, the fact that the Thread has a Looper does not change the situation.
So the important think here is to stop every Thread that you started manually.
As an addition:
You can always check your running Threads in Eclipse. Just attach the
debugger and go to the Debug view. All Threads will be listed there.
Take a look at HandlerThread.
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).