android


How to stop or destroy a running Thread


I have one thread class which star in onCreate of an activity.
class MyThread extends Thread
{
void run()
{
//My code which takes time.
}
}
//-------------------------- To run the thread
MyThread mThread = new MyThread();
mThread.start();
On some external events i need to stop/destroy currently running thread.
And Then i create a new copy of MyThread and call start function on the new thread to run it.
But i am not able to destroy/stop the previous running thread.
Is there any API by which we can destroy running thread.
you cannot destroy...only the android will stop the thread when requires.. you cannot stop or destroy it.. instead try like this..
class MyThread extends Thread
{
void run()
{
while(bool){
//My code which takes time.
}
}
}
//-------------------------- To run the thread
MyThread mThread = new MyThread();
mThread.start();
now when u want to stop the thread... change bool value to false
bool=false;
now your code doesnt run... and you can start new thread...
The Thread.interrupt() method is the accepted way of stopping a running thread, but some implementation details are on you: if your thread is doing IO (or anything else that can throw InterruptedException), it's your responsibility to make sure that you close any resources you have open before you stop your thread.
This page indicates why some of the thread functionality present in earlier versions of Java was deprecated, and why Thread.destroy() was never actually implemented.
You can use AsyncTask insted of Thread, because its known as Painless Threading in android. Once you implement you don't need to bother about Thread Management.
AsyncTask can easily handle and it's very easy for handling ui.
Try something like this..
class MyThread extends Thread
{
void run()
{
//My code which takes time.
}
}
//-------------------------- To run the thread
MyThread mThread = new MyThread();
mThread.start();
// PERFORM ALL YOUR OPERATIONS HERE..
mThread.destroy();

Related Links

Android Saving lot of bitmap into files
Layout for representing images in a circle
Facebook Style Cross Button to remove chat head in android
How to know about the apps installed on the android device
nine-patch images with 1px width or height — special case or faulty files?
Android Pinch Zoom ImageView (PhotoView) with ScrollView
How to set TextView by XML
progressDialog and AsyncTask
Widget with buttons opening applications
Remove OnScrollChangedListener from ViewThreeObserver
AVD will not launch on Ubuntu 14.04
Use activity with include tag on android
add image information bitmap to jpeg
Save files locally, but prevent them from showing up in the users file manager
Cordova cannot add Android failed with exit code ENOENT
Compare two voice in android

Categories

HOME
dns
flex
zend-framework
hessian
edge
big-o
limit
hpc
spring-session
performancepoint
categorical-data
php-5.6
favicon
comsol
docx
fileserver
topology
solrcloud
sar
ember-cli
react-toolbox
dapper-extensions
pic32
fable-f#
google-cast
anki
postback
mockjax
simplecv
sammy.js
vue-chartjs
getlasterror
openerp-7
sim-toolkit
xcode7.3
google-earth-engine
doc
vmware-fusion
preloader
mongotemplate
zenhub
video-conferencing
ngmaterial
libjpeg
mouseleave
structuremap4
pocketsphinx-android
visual-studio-2008-sp1
seek
riot
query-by-example
database-relations
false-sharing
directdraw
apache-commons-daemon
mruby
pseudo-class
amslidemenu
scrypt
unit-of-work
poker
jquery-1.9
android-icons
traceability
stacky
server-administration
argouml
pyunit
rmiregistry
infobright
agility.js
updatesourcetrigger
message-passing
strtod
commercial-application

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App