一.多線程 ???? 1.基本概念 ???????? 進程:正在運行中的程序,一個進程中至少包含一個線程 ???????? 線程:進程的任務,執行任務的一個通道,一個進程中可以包含多個線程 ???? 2.多線程執行的特點: ???????? 兩種方式:分時調度/搶占式調度(java屬于搶占) 二.Thread 類(java.lang) ???? 1.概述:使用該類表示多線程對象,只要創建一個Thread對象,就是一個線程對象產生了 ???? 2.定義:public class Thread extends Object implements Runnable ???? 3.構造方法: ???????? Thread():無參構造,分配新的 Thread 對象 ???????? Thread(String name):使用指定的名稱分配新的 Thread 對象,設置線程名稱 ???????? Thread(Runnable target):接收Runnable接口子類對象,實例化Thread對象 ???????? Thread(Runnable target, String name):接收Runnable接口子類對象,實例化Thread對象,并設置線程名稱 ???? 4.靜態方法: ???????? public static Thread currentThread(){}:返回目前正在執行的線程 [線程名,優先級,] ???????? public static void sleep(long millis) throws InterruptedException{}:使當前線程休眠多少毫秒 ???????? public static void yield(){}:將目前執行的線程暫停,允許其它線程執行 ???? ???? 5.常用方法: ???????? public void run(){}:如果該線程是使用獨立的 Runnable 運行對象構造的,則調用該 Runnable 對象的 run ???????????????????????????? 方法;否則,該方法不執行任何操作并返回。<所以該方法應自覺重寫!!!> ???????? public void start(){}:使該線程開始執行;Java 虛擬機調用該線程的 run 方法 ???????? public final String getName(){}:返回線程的名稱 ???????? public final int getPriority(){}:返回線程優先級 ???????? public final void setName(String name){}:設定線程名稱 ???????? public final void setPriority(int newPriority){}:設定線程的優先值 ???????? public final ThreadGroup getThreadGroup(){}:Returns the thread group to which this thread belongs. ???????????????????????????????????????????? This method returns null if this thread has died (been stopped). ???? 代碼演示: ???????
六.ThreadGroup 類(java.lang) ???? 簡介:線程組:java允許對一批線程進行管理,使用ThreadGroup表示線程組,所有線程均有指定線程組,如果沒有顯式指定,則為默認線程組.默認情況下,子線程和創建他的父線程 ???????? 屬于同一線程組.一旦某線程加入指定線程組內,該線程會一直屬于該組,直至死亡,運行過程中不可改變. ???? 繼承關系:java.lang.Object--java.lang.ThreadGroup ???? 定義:public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandler ???? 構造器: ???????? ThreadGroup(String name): Constructs a new thread group. ???????? ThreadGroup(ThreadGroup parent, String name): Creates a new thread group. ???? 常用方法: ???????? public int activeCount(){}:Returns an estimate of the number of active threads in this thread group and its subgroups ???????? public int activeGroupCount(){}:Returns an estimate of the number of active groups in this thread group and its subgroups. ???????????????????????????????????????? Recursively iterates over all subgroups in this thread group. ???????? public int enumerate(Thread[] list) Throws SecurityException{}: ???????? public int enumerate(Thread[] list,boolean recurse)Throws SecurityException{}:Copies into the specified array every active thread ???????????????????????????????????????????????? in this thread group. If recurse is true, this method recursively enumerates all subgroups of ???????????????????????????????????????????????? this thread group and references to every active thread in these subgroups are also included. ???????????????????????????????????????????????? If the array is too short to hold all the threads, the extra threads are silently ignored. ???????? public final String getName(){}:Returns the name of this thread group. ???????? public final ThreadGroup getParent()Throws SecurityException{}:Returns the parent of this thread group. ???????? public final boolean isDaemon(){}:Tests if this thread group is a daemon thread group ???????? public final void checkAccess() Throws SecurityException{}:Determines if the currently running thread has permission to modify this thread group. ???????? public final void setDaemon(boolean daemon)Throws SecurityException{}:Changes the daemon status of this thread group.??? ???? ???? 代碼演示:獲取當前系統內運行的所有線程組及線程名 ???????
七.Executor 接口(java.io.concurrent) 簡介:線程池:由于線程涉及到與操作系統交互,所以啟動一個新線程的成本比較高,因此,java提供了線程池機制來提高性能,尤其是當程序中需要大量生存期很短暫的線程時,應該考慮 ???? 使用線程池.所謂線程池是指在系統啟動時即創建大量空閑的線程,程序將一個Runnable對象或Callable對象傳給線程池,線程池就會啟動一個線程來執行他們的run或call方法,當方法 ???? 結束時,線程并不會死亡,而是返回線程池成為空閑狀態,等待執行下一個任務 定義: public interface Executor 方法:public void execute(Runnable command) Throws RejectedExecutionException | NullPointerException {}:Executes the given command at some time in the future 實現類:AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor 子接口:ExecutorService, ScheduledExecutorService