1. 程式人生 > >大發彩票源碼搭建修復采集

大發彩票源碼搭建修復采集

final thread 等待 數據 搭建 沒有 技術 界面 type

大發彩票源碼搭建修復采集
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 開始執行");@Override
br/>@Override
//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 緩存進程
註:進程的重要級別,在於運行過程中有可能隨時變化

大發彩票源碼搭建修復采集