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 IBinder redelivery makes complex multiple client connections?
Using fragments for TAB layout, how to use them?
Create android module library from existing source
How to insert image into activity
RecyclerView GridLayoutManager with full width header
Yii2 Extension Jeapie for Push Notification
android proper implementation for deep linking
Edittext automatically gets focus when move between pages in a view pager
Android: Custom animation for ViewPager
How to secure an Android application having Guest login (without any credentials) as Token Based authentication can't be applied
Android - create signed APK with Proguard
Changing value of textview using android spinners
Upgrade Kotlin from 1.0.2 - 1.0.3: java.lang.NoSuchMethodError: No virtual method provideInjectedData$app_compileDebugKotlin()
Multiple Activities in Android manifest/app drawer [duplicate]
How do I pass data back to an activity that starts the activity which passes data to it?
How specify sign in button scopes in xml?

Categories

HOME
localization
string
telegram
pda
x264
data-visualization
odoo-8
modal-dialog
elasticsearch-5
rcloud
distribution
watson-conversation
x-cart
opengl-es-3.0
scrolltop
intershop
mathprog
google-data-studio
html5-appcache
babel
question2answer
string-concatenation
tosca
bulletphysics
android-sharedpreferences
delimiter
oculus
inversion-of-control
bringtofront
mql
google-earth
kudu
code-snippets
fossil
sharding
html-encode
sql-delete
nohup
typesafe-config
xv6
plr
tcc
android-exoplayer
photos
automapper-5
facebook-pixel
openblas
page-replacement
colorbar
sundials
dt
py2neo
swingworker
juice-ui
rocks
redistributable
lean
np-spring
roadkill-wiki
canalyzer
ms-access-2000
matlab-deployment
dot.js
broadband
cosine-similarity
boost-program-options
mruby
sip-server
sql-scripts
appserver
wcm
insight.database
codeplex
wxformbuilder
tinyxml
koala
amslidemenu
getopt
xmltodict
sphinxql
embedded-fonts
epiceditor
oracle-enterprise-linux
drawtobitmap
cgbitmapcontextcreate
event-receiver
unattended-processing
silent
hbm2java
asynchronous-wcf-call

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