天天看點

Android 多線程實作異步執行demo,線程池使用demo方法1:方法2線程池使用:

方法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;
    }

    @Override
    public 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方法1:方法2線程池使用: