大发彩票源码搭建修复采集

大发彩票源码搭建修复采集
hubawl.com

大发彩票源码搭建修复采集

线程阻塞:
当一个应用程序启动之后,android系统会为这个应用程序创建一个主线程,这个线程非常重要。他负责渲染视图,分发事件到相应监听并执行,对界面进行轮询的监听。因此,一般也叫作“UI”线程。android 系统不会给应用程序的多个元素组件,建立多个线程来执行。一个视图(activity)中的多个view组件运行在同一个UI线程当中,因此,多个view组件的监听器在执行可能会相互影响。
在UI线程当中执行耗时操作时则会出现卡死,如访问网络,访问数据库等,就会造成线程阻塞。
以下例子都是点击button2看是否会对button1有影响
例子:
//为button添加一个动画操作
Button button1 = (Button) findViewById(R.id.button1);
TranslateAnimation animation = new TranslateAnimation(0, 150,0,0);
animation.setRepeatCount(30); //重复多少次
animation.setDuration(2000); //完成一次动画需要多长时间
button1.setAnimation(animation);
//实例button2
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
//点击button2实现耗时操作,耗时5秒 可以造成线程阻塞
try {
Thread.sleep(5000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
如何不引起阻塞:
1、创建一个新的线程
button2.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
int sum = 10; //通过耗时操作计算出来的一个值,把这个值显示到button上
TextView view = (TextView) v;
view.setText(""+10);
}
}).start();
}
}
2、用post 由于开始执行--执行结束---post执行是这个顺序,所以根本没有占用系统的线程,不会阻塞
button2.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
System.out.println("--------"+Thread.currentThread().getId());
new Thread(new Runnable() {
public void run() {
//System.out.println(" 1 线程开始执行"); 开始执行--执行结束---post执行 这个顺序
try {
Thread.sleep(5000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
int sum = 10; //通过耗时操作计算出来的一个值,把这个值显示到button上
v.post(new Runnable() {
//System.out.println("post 开始执行");br/>@Override
public void run() {
//System.out.println(" 3 post 开始执行");
// TODO Auto-generated method stub
TextView view = (TextView) v;
view.setText(""+10);
}
});
//System.out.println("2 线程执行结束");
}
}).start();
}
}
3、AsyncTask 相当于封装好的方法,post可读性和维护性差
在onCreate方法前加上代码:private Button button2 = null;
在onCreate方法中添加如下代码:
//位button添加一个动画操作
Button button1 = (Button) findViewById(R.id.button1);
//jquery animate

    TranslateAnimation animation = new TranslateAnimation(0, 150,0,0);
    animation.setRepeatCount(30);  //重复多少次
    animation.setDuration(2000);  //完成一次动画需要多长时间
    button1.setAnimation(animation);

    //实例button2
    button2 = (Button) findViewById(R.id.button2);

    //Ui线程阻塞       如果超过5秒的话系统就会弹出一直等待还是退出
    //当点击button2的时候会耗时5秒,这样会影响button1,使其卡死了,5秒过后又重新执行,这个过程叫做线程阻塞,
    button2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
new MyAsyncTask().execute();
}
});
引用封装好的类:
private class MyAsyncTask extends AsyncTask
{
protected Integer doInBackground(String...urls) {
try {
Thread.sleep(5000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
int sum = 10;

    return sum;

}
protected void onPostExecute(Integer sum) {
//调用上面的主键
button2.setText(""+sum);
}
}

优先级
操作系统 1:n 进程 1:n 线程
当android应用启动时,系统会启动一个进程和一个主线程来运行这个应用。当进程很多占用内存很大时,会考虑移出这些进程,回收内存。
因此,在android进程管理中,设置了进程优先级别。优先级决定进程内运行的程序以及程序的状态。当回收内存时会杀掉一些优先级的进程
五个优先级:
1、Foreground process 用户正在操作的界面(最高)
2、Visible process 用户从一个程序切换到另一个程序,旧的程序会被替换掉,但是可恢复
3、Service process 边放音乐,边看电子书,音乐就是service
4、Background process 定时检测更新
5、Empty process 缓存进程
注:进程的重要级别,在于运行过程中有可能随时变化

猜你喜欢

转载自blog.51cto.com/13839990/2133219