Android 线程将停止,直到方法的执行完成。(安卓)
我正在尝试使用一个线程来做一个Android游戏,该线程会重复一个循环来进行绘制、移动和其他操作 我在执行方法时遇到了一个问题,该方法使用“do while”循环搜索值。当执行此方法时,线程不会继续,直到此过程结束 避免这种情况的最佳选择是什么?在该方法中创建另一个线程?如果你能举个例子,我会非常感激 下面是一些伪代码:Android 线程将停止,直到方法的执行完成。(安卓),android,runnable,ui-thread,Android,Runnable,Ui Thread,我正在尝试使用一个线程来做一个Android游戏,该线程会重复一个循环来进行绘制、移动和其他操作 我在执行方法时遇到了一个问题,该方法使用“do while”循环搜索值。当执行此方法时,线程不会继续,直到此过程结束 避免这种情况的最佳选择是什么?在该方法中创建另一个线程?如果你能举个例子,我会非常感激 下面是一些伪代码: void mainLoop(){ drawElements(); moveElements(); //... //... reposit
void mainLoop(){
drawElements();
moveElements();
//...
//...
reposition();
}
void reposition(){
// this stops my thread
do{
// do stuff
}while(!end);
// do stuff
}
如果您仍然需要阻塞并等待循环完成搜索,那么创建另一个线程对您没有帮助。问题实际上是“do stuff”循环中发生了什么,您只需要优化它就可以解决问题。如果您仍然需要阻止并等待循环完成搜索,那么创建另一个线程对您没有帮助。问题实际上是“do stuff”循环中发生了什么,您只需要优化它就可以解决问题。如果您仍然需要阻止并等待循环完成搜索,那么创建另一个线程对您没有帮助。问题实际上是“do stuff”循环中发生了什么,您只需要优化它就可以解决问题。如果您仍然需要阻止并等待循环完成搜索,那么创建另一个线程对您没有帮助。问题实际上是在“do stuff”循环中发生了什么,您只需要优化它来解决问题。使用asyntask,并在asyntask的
doInBackground中使用它,执行线程工作,并在asyntask的onPostExecute
中调用重新定位方法
使用asyntask,在asyntask的doInBackground
中执行线程工作,在asyntask的onPostExecute
中调用重新定位方法使用asyntask并在asyntask的doInBackground
,执行线程工作,并在asyntask的onPostExecute
中调用重新定位方法
使用asyntask,在asyntask的doInBackground
中执行线程工作,并在asyntask的onPostExecute
中调用重新定位方法
,如wqrahd建议使用AsyncTask。
我假设mainLoop是一个主UI线程
公共类重新定位类扩展异步任务{
private Context mContext;
public RepositionClass(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
// do UI related here, this function will run in main thread context.
}
@Override
protected Boolean doInBackground(String... params) {
// call non-ui(computation intensive) part of reposition function here.
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// do UI related part of reposition function here.
}
}根据wqrahd的建议,使用异步任务。
我假设mainLoop是一个主UI线程
公共类重新定位类扩展异步任务{
private Context mContext;
public RepositionClass(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
// do UI related here, this function will run in main thread context.
}
@Override
protected Boolean doInBackground(String... params) {
// call non-ui(computation intensive) part of reposition function here.
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// do UI related part of reposition function here.
}
}根据wqrahd的建议,使用异步任务。
我假设mainLoop是一个主UI线程
公共类重新定位类扩展异步任务{
private Context mContext;
public RepositionClass(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
// do UI related here, this function will run in main thread context.
}
@Override
protected Boolean doInBackground(String... params) {
// call non-ui(computation intensive) part of reposition function here.
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// do UI related part of reposition function here.
}
}根据wqrahd的建议,使用异步任务。
我假设mainLoop是一个主UI线程
公共类重新定位类扩展异步任务{
private Context mContext;
public RepositionClass(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
// do UI related here, this function will run in main thread context.
}
@Override
protected Boolean doInBackground(String... params) {
// call non-ui(computation intensive) part of reposition function here.
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// do UI related part of reposition function here.
}
}