Android 多线程实现异步执行demo,线程池使用demo
方法1:
1、常見Runnable對象設置同步代碼run運行體
class AutoSaleTicket implements Runnable {private int ticket = 20;public void run() {while (true) {// 循環是指線程不停的去賣票// 當操作的是共享數據時,用同步代碼塊進行包圍起來,這樣在執行時,只能有一個線程執行同步代碼塊里面的內容synchronized (this) {if (ticket > 0) {// 不要在同步代碼塊里面sleep,作用只是自已不執行,也不讓線程執行System.out.println("lgq"+Thread.currentThread().getName()+ " 賣出 第 " + (20 - ticket + 1) + " 張票");ticket--;} else {break;}}// 所以把sleep放到同步代碼塊的外面,這樣賣完一張票就休息一會,讓其他線程再賣,這樣所有的線程都可以賣票try {Thread.sleep(200);} catch (Exception ex) {}}} }2、創建多線程,啟動多線程
AutoSaleTicket ticket = new AutoSaleTicket(); Thread t1 = new Thread(ticket, "11東城代售"); Thread t2 = new Thread(ticket, "22西城代售"); Thread t3 = new Thread(ticket, "33朝陽代售"); Thread t4 = new Thread(ticket, "44海淀代售"); t1.start(); t2.start(); t3.start(); t4.start();3、多線程同步執行結果
03-22 15:40:43.167 9967-10272/com.tianxin.httpheader I/System.out: lgq11東城代售 賣出 第 1 張票
03-22 15:40:43.167 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝陽代售 賣出 第 2 張票
03-22 15:40:43.167 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 賣出 第 3 張票
03-22 15:40:43.167 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 賣出 第 4 張票
03-22 15:40:43.368 9967-10272/com.tianxin.httpheader I/System.out: lgq11東城代售 賣出 第 5 張票
03-22 15:40:43.370 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 賣出 第 6 張票
03-22 15:40:43.370 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝陽代售 賣出 第 7 張票
03-22 15:40:43.371 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 賣出 第 8 張票
03-22 15:40:43.570 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 賣出 第 9 張票
03-22 15:40:43.571 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 賣出 第 10 張票
03-22 15:40:43.572 9967-10272/com.tianxin.httpheader I/System.out: lgq11東城代售 賣出 第 11 張票
03-22 15:40:43.572 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝陽代售 賣出 第 12 張票
03-22 15:40:43.771 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 賣出 第 13 張票
03-22 15:40:43.772 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 賣出 第 14 張票
03-22 15:40:43.773 9967-10272/com.tianxin.httpheader I/System.out: lgq11東城代售 賣出 第 15 張票
03-22 15:40:43.773 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝陽代售 賣出 第 16 張票
03-22 15:40:43.973 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 賣出 第 17 張票
03-22 15:40:43.973 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 賣出 第 18 張票
03-22 15:40:43.974 9967-10272/com.tianxin.httpheader I/System.out: lgq11東城代售 賣出 第 19 張票
03-22 15:40:43.974 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝陽代售 賣出 第 20 張票
?
方法2線程池使用:
創建
class MyTask implements Runnable {private int taskNum;public MyTask(int num) {this.taskNum = num;}@Overridepublic void run() {System.out.println("lgq正在執行task "+taskNum);try {Thread.currentThread().sleep(4000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("lgqtask "+taskNum+"執行完畢");} }執行
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(5));for(int i=0;i<15;i++){MyTask myTask = new MyTask(i);executor.execute(myTask);System.out.println("lgq線程池中線程數目:"+executor.getPoolSize()+",隊列中等待執行的任務數目:"+executor.getQueue().size()+",已執行玩別的任務數目:"+executor.getCompletedTaskCount()); } executor.shutdown();結果
總結
以上是生活随笔為你收集整理的Android 多线程实现异步执行demo,线程池使用demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 判定1个IP地址是否是合法IP
- 下一篇: Android listview优化以及