最近学习Android的时候,需要理解Android线程,我就模拟了一下线程阻塞
科普下UI线程:
当一个应用程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事
件以及屏幕绘图事件,并把相关的事件分发到对应的组件进行处理。所以主线程又被叫做UI线程
一、在Activity中拖拽两个按钮,第一个按钮设置动画,第二个按钮设值
XML:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/yidongBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平行移动"/>
</LinearLayout>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="@dimen/activity_vertical_margin"
android:text="变形" />
</LinearLayout> Activity:
public class MainActivity extends Activity {
private Button yidongBtn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yidongBtn=(Button)findViewById(R.id.yidongBtn);
//给btn设置动画
TranslateAnimation animation=new TranslateAnimation(0,1500,0,0);
animation.setRepeatCount(20);
animation.setDuration(2000);
yidongBtn.setAnimation(animation);
final Button btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//设置其耗时操作
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Log.i("线程沉睡",e.getMessage());
}
int sum=10;
btn2.setText("计数:"+sum);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
---当在应用程序启动时,界面上的“平行移动按钮”就已经在每隔2s向右平行移动1500dp,要移动20次,在这个动作发生时,我们并行点击“变形”按钮,会发现应用程序界面卡死,这时就说UI线程阻塞。
那么UI线程阻塞的定义是啥呢?
百度讲:----线程阻塞:一个线程在执行过程中暂停,以等待某个条件的触发
从这句话来理解,现在我们这个小应用程序也确实满足这个情况,当我们“平行移动按钮”在运行过程中,我们在点击“变形”按钮时,“平行移动按钮”被暂停,他只能等待“变形”按钮动作完成之后,才会继续“平行移动按钮”在规定次数,规定的时间未完成的动作。
而我们要的效果是,二者不相互影响。而且这个线程阻塞说明了,“平行移动"按钮占用了一个线程,而“变形”按纽也占用了一个线程,这个与Android官网给出的API中提出的两点是违背的;而Android应用程序当开启时,他会创建一个进程PID,(我们可以在LogCat中的看到PID的值)那就表明Android的是属于单例模式。
Android官方网站的API指出:
There are basically two main ways of having a Thread execute application code.
One is providing a new class that extends Thread and overriding its run() method.
The other is providing a new Thread instance with a Runnable object during its creation. In both cases, the start() method must be called to actually execute the new Thread. 啥意思呢?
中文就讲了两个意思:
1.不要阻塞UI线程
2.不要在UI线程之外的其他线程中,对视图中的组件进行设置
假设用户违反了第一条,也就是不给“变形”按钮设值,那么就是假死,用户体验度差呗!
但是,当我们加上了
final Button btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//1.不要阻塞UI线程(这里我不阻塞线程,我重新new线程)
new Thread(new Runnable() {
@Override
public void run() {
//设置其耗时操作
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Log.i("耗时操作",e.getMessage());
}
int sum=10;
btn2.setText("计数:"+sum);
}
}).start();
}
});
运行应用程序弹出出错对话框:

LogCat后台出现的错误:

(此时new的时候,我便是不在UI线程中执行修改操作)
加上红色圈圈里面的内容时,则LogCat中抛出异常:
Onlytheoriginalthreadthatcreatedaviewhierarchycantouchitsviews:只有创建View的那个线程才能对其修改、设值。只有创建的线程才能完成其修改,也就是说只有主线程才能对其进行修改,也就是说Android系统中的视图组件并不是线程安全的,如果要更新视图,必须在主线程中更新,不可以在子线程中执行更新的操作。
那么该肿么办呢?
|