Hes*_*sam 5 multithreading android handler
我有以下代码.
public class SplashScreen extends Activity {
private int _splashTime = 5000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Thread(){
@Override
public void run(){
Intent mainMenu = new Intent(SplashScreen.this, MainMenu.class);
SplashScreen.this.startActivity(mainMenu);
SplashScreen.this.finish();
overridePendingTransition(R.drawable.fadein, R.drawable.fadeout);
}
}, _splashTime);
}
}
Run Code Online (Sandbox Code Playgroud)
我在分析这段代码时遇到了问题.至于知道处理程序在主线程中运行.但它有在其他线程中运行的线程.
MainMenu.class将在主线程或第二个线程中运行?如果主线程停止5秒,ANR将被提升.为什么当我停止延迟(_splashTime)ANR时不显示(即使我将它增加到超过5秒)
Com*_*are 12
至于知道处理程序在主线程中运行.
对象不在线程上运行,因为对象不运行.方法运行.
但它有在其他线程中运行的线程.
您尚未发布涉及任何"其他线程"的任何代码.上面代码清单中的所有内容都与流程的主应用程序线程相关联.
MainMenu.class将在主线程或第二个线程中运行?
对象不在线程上运行,因为对象不运行.方法运行.MainMenu似乎是一个Activity.onCreate()在主应用程序线程上调用活动生命周期方法(例如).
为什么我延迟停止它(_splashTime)ANR不显示(即使我将它增加到超过5秒)
你不是"延迟[主应用程序线程]".您已安排Runnable在延迟_splashTime毫秒后在主应用程序线程上运行a .但是,postDelayed()不是阻止呼叫.它只是在事件队列上放置一个事件,该事件将在_splashTime几毫秒内不执行.
另外,请更换Thread用Runnable,因为postDelayed()不使用Thread.你的代码编译和运行,因为Thread实现Runnable,但你会混淆自己,认为使用Thread而不是Runnable意味着你的代码将在后台线程上运行,而它不会.
| 归档时间: |
|
| 查看次数: |
4984 次 |
| 最近记录: |