Android 多線程小結

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

1、啟動歡迎介面

 1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3     // TODO Auto-generated method stub 4     super.onCreate(savedInstanceState); 5     setContentView(R.layout.welcome); 6     new Handler().postDelayed(r, 1000);// 1秒後關閉,並跳轉到首頁面 7 } 8  9 Runnable r = new Runnable() {10 11     @Override12     public void run() {13     // TODO Auto-generated method stub14     Intent intent = new Intent();15     intent.setClass(WelcomeActivity.this, MainActivity.class);16     startActivity(intent);17     finish();18     }19 };
View Code

2、遠端資料庫查詢

 1 // 主線程啟動資料庫查詢線程 2         Thread t = new Thread(QueryDB); 3         t.start(); 4         // 等待子線程結束 5         try { 6             ((Thread) t).join(); 7         } catch (InterruptedException e) { 8             e.printStackTrace(); 9         }10         listview.setAdapter(adapter);11         listview.setVisibility(View.VISIBLE);12         listview.setItemsCanFocus(false);13 14 15     // 資料庫查詢子線程,共用資料16     Runnable QueryDB = new Runnable()17     { 18         @Override19         public void run()20         {21             // 此處寫訪問  webservice 代碼22             List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();23             list = dbUtil.getAllInfo();24             // ListItem25             adapter = new SimpleAdapter(26                     SpeLineListActivity.this, 27                     list, 28                     R.layout.v1_speline_item, 29                     new String[] { "Cno", "Cname", "Cnum" }, 30                     new int[] { R.id.txt_company_name, R.id.txt_sl_line, R.id.txt_sl_contact }31                     );32 33         }34     };
View Code

3、多線程查詢

1 List<Thread> tList = new ArrayList<Thread>();2 for (int i = 0; i < tList.size(); i++) {3 try {4     tList.get(i).join();5     } catch (InterruptedException e) {6     e.printStackTrace();7     }8 }
View Code

 

Android 多線程小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.