这是 Android 线程的安全用法吗?
Is this a safe usage of Android Threading?
我在 Android 中使用原版 Android SDK 和线程创建了一个游戏。我的大部分游戏逻辑都包含在 Runnable 中,这些逻辑被发布到 Handlers 或 Views。
例如:
Post 可运行到视图并在可运行视图中编辑该视图
private int currentRotation =0;
private Runnable rotateRunnable = new Runnable(){
@Override
public void run() {
currentRotation+=DEFAULT_ROTATION_SPEED * direction;
Gravity_MeteorView.this.setRotation(currentRotation);
Gravity_MeteorView.this.postIfAlive(this,2*MovingView.HOW_OFTEN_TO_MOVE);
}
};
public Gravity_MeteorView(Context context) {
super(context);//ImageView
//...
this.post(rotateRunnable);
}
Post 一个 Runnable 到处理程序并在 Runnable 中编辑一些视图
Handler spawningHandler = new Handler();
public final void spawnMeteorShower(final int numMeteors,final int millisecondsBetweenEachMeteor) {
spawningHandler.post(
new Runnable(){
private int numSpawned=0;
@Override
public void run() {
Gravity_MeteorView bob = new Gravity_MeteorView(context);
someLayout.addView(bob);
//...
if(numSpawned<numMeteors){
spawningHandler.postDelayed(this,millisecondsBetweenEachMeteor);
}
}
});
}
在大多数情况下一切正常。流星不断地产生和旋转。虽然非常非常罕见,流星会生成但不可见且不受伤害。我想知道这些奇怪的错误是否可能是由于我对游戏线程处理不当造成的。
我在 Android 文档中注意到
[modifying] the ImageView from the worker thread instead of the UI thread...can result in undefined and unexpected behavior, which can be difficult and time-consuming to track down.
http://developer.android.com/guide/components/processes-and-threads.html
似乎第一个片段可能没问题,而第二个片段可能会导致问题(如果它不是 运行 在 UI 线程或其他东西上)。我是否违反了这里的 Android 最佳实践?
谢谢!
It seems like the first snippet may be fine, while the second could cause problems (if it's not running on UI thread or something). Am I violating Android best practices here?
spawningHandler
附加到 UI 线程 Looper
,因为您在那里使用 View
。否则你会崩溃,因为你不能从工作线程中做到这一点。
它是线程安全的,因为没有并行执行。所有 Runnable
你 post 都去同一个 MessageQueue
。我在您 post 编辑的代码中没有看到线程问题。
我在 Android 中使用原版 Android SDK 和线程创建了一个游戏。我的大部分游戏逻辑都包含在 Runnable 中,这些逻辑被发布到 Handlers 或 Views。
例如:
Post 可运行到视图并在可运行视图中编辑该视图
private int currentRotation =0;
private Runnable rotateRunnable = new Runnable(){
@Override
public void run() {
currentRotation+=DEFAULT_ROTATION_SPEED * direction;
Gravity_MeteorView.this.setRotation(currentRotation);
Gravity_MeteorView.this.postIfAlive(this,2*MovingView.HOW_OFTEN_TO_MOVE);
}
};
public Gravity_MeteorView(Context context) {
super(context);//ImageView
//...
this.post(rotateRunnable);
}
Post 一个 Runnable 到处理程序并在 Runnable 中编辑一些视图
Handler spawningHandler = new Handler();
public final void spawnMeteorShower(final int numMeteors,final int millisecondsBetweenEachMeteor) {
spawningHandler.post(
new Runnable(){
private int numSpawned=0;
@Override
public void run() {
Gravity_MeteorView bob = new Gravity_MeteorView(context);
someLayout.addView(bob);
//...
if(numSpawned<numMeteors){
spawningHandler.postDelayed(this,millisecondsBetweenEachMeteor);
}
}
});
}
在大多数情况下一切正常。流星不断地产生和旋转。虽然非常非常罕见,流星会生成但不可见且不受伤害。我想知道这些奇怪的错误是否可能是由于我对游戏线程处理不当造成的。
我在 Android 文档中注意到
[modifying] the ImageView from the worker thread instead of the UI thread...can result in undefined and unexpected behavior, which can be difficult and time-consuming to track down.
http://developer.android.com/guide/components/processes-and-threads.html
似乎第一个片段可能没问题,而第二个片段可能会导致问题(如果它不是 运行 在 UI 线程或其他东西上)。我是否违反了这里的 Android 最佳实践?
谢谢!
It seems like the first snippet may be fine, while the second could cause problems (if it's not running on UI thread or something). Am I violating Android best practices here?
spawningHandler
附加到 UI 线程 Looper
,因为您在那里使用 View
。否则你会崩溃,因为你不能从工作线程中做到这一点。
它是线程安全的,因为没有并行执行。所有 Runnable
你 post 都去同一个 MessageQueue
。我在您 post 编辑的代码中没有看到线程问题。