Android 多執行緒

2021-09-29 02:33:24 字數 1377 閱讀 9698

1,常用的thread 和running的方法 

public static void thread()

});thread.start();

}private static void running()

};thread thread = new thread(runnable);

thread.start();

}private static void threadfactory()

};runnable runnable = new runnable()

};thread thread1 = threadfactory.newthread(runnable);

thread thread2 = threadfactory.newthread(runnable);

thread1.start();

thread2.start();

}

2,android 最常用的executor方法

private static void executor()

};executor executor = executors.newcachedthreadpool();

executor.execute(runnable);

executor.execute(runnable);

executor.execute(runnable);

}

3,自定義共用執行緒池

// 自定義共用執行緒池

blockingdequequeue = new linkedblockingdeque<>(10000);

executor threadpool = new threadpoolexecutor(5,100,5l, timeunit.seconds,queue);

4,又返回值的後台

// 又返回值的後台

static void callable()

};executorservice executor = executors.newcachedthreadpool();

futurefuture = executor.submit(callable);

try catch (interruptedexception e) catch (executionexception e)

}

5,其他

synchronized  volatile lock atomicinteger(自增)

reentrantreadwritelock(讀寫鎖):讀的時候,other可以讀,但不可以寫;寫的時候,other讀和寫都不可以

Android 多執行緒

1 簡介 2 android 平台下的多執行緒 package com.powerise.thread import android.os.bundle import android.view.view import android.view.view.onclicklistener import ...

Android 多執行緒

最近開始做安卓專案,然而對多執行緒的理解和應用還是欠缺,最近就系統的學習一下 public class testthreadactivity extends baseactivity private void initviews private class mytask extends asynct...

android學習 多執行緒

一建立執行緒的兩種方法 1 通過thread類的構造方法建立執行緒 thread runnable runnable 該構造方法的引數runnable可以通過建立乙個runnable類的物件並重寫其run 方法來實現,例如 thread thread new thread new runnable ...