日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 多线程实现异步执行demo,线程池使用demo

發布時間:2023/12/10 Android 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。