Android线程不会停止运行
我有一个执行 Web 请求的基本异步任务。该线程不包含在循环或任何其他内容中,它执行请求并从 run()
返回。当我尝试使用该线程执行另一个请求时,我会抛出异常,因为该线程已经在运行。我在这个网站上搜索了很多答案,但似乎出现的只是停止循环中的线程,基本上是强制线程返回。
我是否应该将线程中的请求代码放入一个循环中,等待主线程中的某种标志来告诉它继续并再次执行?像:
public void run()
{
while ( threadIsStillRunning )
{
while ( !threadShouldExecute )
{
//Sleep the thread
}
//Execute the request
}
}
编辑: 好的,这是线程(它包含在我的类对象之一 - WebServiceHelper 中):
private Thread executeRequest = new Thread()
{
public void run()
{
//Meat of the code
isRunning = false;
}
}
然后我在同一个类中拥有另一个类方法(WebServiceHelper):
private volatile boolean isRunning = false;
public void Execute(WebServiceHandler handler)
{
while ( isRunning )
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
isRunning = true;
r = handler;
executeRequest.start();
}
其中 r 只是一个接口对象,我用它来执行对该对象的回调执行请求。
然后在我的主要活动(请求线程执行的活动)中,我有这样的:
private Runnable getSiteData = new Runnable(){
public void run(){
mWebServiceHelper.SetMethod("GetSiteData");
mWebServiceHelper.Execute(mySiteHelper);
}
};
public void downloadDidFinish(List<Map<String, String>> data)
{
// TODO Auto-generated method stub
TeamList.StoreTeams(data );
mHandler.post(getSiteData);
}
完成后上面的线程调用 downloadDidFinish
,然后我立即执行另一个请求,如您所见。崩溃正在发生当我尝试在 WebServiceHelper
上再次调用 Execute
并再次启动线程时。
I have a basic asynchronous task that performs a web request. The thread is not contained in a loop or anything, it performs the request and returns from run()
. When I try to execute another request, using that thread, I get an exception thrown because the thread is already running. I've searched around a lot on this site for answers, but all that seems to come up is stopping threads that are in a loop, basically forcing the thread to return.
Should I just put the request code in the thread into a loop that waits on some kind of flag from the main thread to tell it to go ahead and execute again? like:
public void run()
{
while ( threadIsStillRunning )
{
while ( !threadShouldExecute )
{
//Sleep the thread
}
//Execute the request
}
}
EDIT:
Ok, well here's the thread (this is contained in one of my class objects-WebServiceHelper):
private Thread executeRequest = new Thread()
{
public void run()
{
//Meat of the code
isRunning = false;
}
}
I then have another class method in the same class(WebServiceHelper):
private volatile boolean isRunning = false;
public void Execute(WebServiceHandler handler)
{
while ( isRunning )
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
isRunning = true;
r = handler;
executeRequest.start();
}
where r is just an interface object that I use to perform callbacks to the object performing the request.
Then in my main activity (the one that requested the thread execution i have this:
private Runnable getSiteData = new Runnable(){
public void run(){
mWebServiceHelper.SetMethod("GetSiteData");
mWebServiceHelper.Execute(mySiteHelper);
}
};
public void downloadDidFinish(List<Map<String, String>> data)
{
// TODO Auto-generated method stub
TeamList.StoreTeams(data );
mHandler.post(getSiteData);
}
downloadDidFinish
gets called by the thread above upon completion, I then perform another request right after as you can see. The crash is happening when I try to call Execute
again on the WebServiceHelper
and start the thread again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Asynctask 对于管理线程非常有用。
https://developer.android.com/reference/android/os/AsyncTask。 html
https://developer.android.com/resources/articles/painless-threading。 html
这是一个示例: http://labs.makemachine .net/2010/05/android-asynctask-example/
Asynctask is very useful to manage your threads.
https://developer.android.com/reference/android/os/AsyncTask.html
https://developer.android.com/resources/articles/painless-threading.html
Here is an example: http://labs.makemachine.net/2010/05/android-asynctask-example/