一.多線程 ???? 1.基本概念 ???????? 進(jìn)程:正在運(yùn)行中的程序,一個(gè)進(jìn)程中至少包含一個(gè)線程 ???????? 線程:進(jìn)程的任務(wù),執(zhí)行任務(wù)的一個(gè)通道,一個(gè)進(jìn)程中可以包含多個(gè)線程 ???? 2.多線程執(zhí)行的特點(diǎn): ???????? 兩種方式:分時(shí)調(diào)度/搶占式調(diào)度(java屬于搶占) 二.Thread 類(java.lang) ???? 1.概述:使用該類表示多線程對(duì)象,只要?jiǎng)?chuàng)建一個(gè)Thread對(duì)象,就是一個(gè)線程對(duì)象產(chǎn)生了 ???? 2.定義:public class Thread extends Object implements Runnable ???? 3.構(gòu)造方法: ???????? Thread():無參構(gòu)造,分配新的 Thread 對(duì)象 ???????? Thread(String name):使用指定的名稱分配新的 Thread 對(duì)象,設(shè)置線程名稱 ???????? Thread(Runnable target):接收Runnable接口子類對(duì)象,實(shí)例化Thread對(duì)象 ???????? Thread(Runnable target, String name):接收Runnable接口子類對(duì)象,實(shí)例化Thread對(duì)象,并設(shè)置線程名稱 ???? 4.靜態(tài)方法: ???????? public static Thread currentThread(){}:返回目前正在執(zhí)行的線程 [線程名,優(yōu)先級(jí),] ???????? public static void sleep(long millis) throws InterruptedException{}:使當(dāng)前線程休眠多少毫秒 ???????? public static void yield(){}:將目前執(zhí)行的線程暫停,允許其它線程執(zhí)行 ???? ???? 5.常用方法: ???????? public void run(){}:如果該線程是使用獨(dú)立的 Runnable 運(yùn)行對(duì)象構(gòu)造的,則調(diào)用該 Runnable 對(duì)象的 run ???????????????????????????? 方法;否則,該方法不執(zhí)行任何操作并返回。<所以該方法應(yīng)自覺重寫!!!> ???????? public void start(){}:使該線程開始執(zhí)行;Java 虛擬機(jī)調(diào)用該線程的 run 方法 ???????? public final String getName(){}:返回線程的名稱 ???????? public final int getPriority(){}:返回線程優(yōu)先級(jí) ???????? public final void setName(String name){}:設(shè)定線程名稱 ???????? public final void setPriority(int newPriority){}:設(shè)定線程的優(yōu)先值 ???????? 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) ???? 簡(jiǎn)介:線程組:java允許對(duì)一批線程進(jìn)行管理,使用ThreadGroup表示線程組,所有線程均有指定線程組,如果沒有顯式指定,則為默認(rèn)線程組.默認(rèn)情況下,子線程和創(chuàng)建他的父線程 ???????? 屬于同一線程組.一旦某線程加入指定線程組內(nèi),該線程會(huì)一直屬于該組,直至死亡,運(yùn)行過程中不可改變. ???? 繼承關(guān)系:java.lang.Object--java.lang.ThreadGroup ???? 定義:public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandler ???? 構(gòu)造器: ???????? 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.??? ???? ???? 代碼演示:獲取當(dāng)前系統(tǒng)內(nèi)運(yùn)行的所有線程組及線程名 ???????
七.Executor 接口(java.io.concurrent) 簡(jiǎn)介:線程池:由于線程涉及到與操作系統(tǒng)交互,所以啟動(dòng)一個(gè)新線程的成本比較高,因此,java提供了線程池機(jī)制來提高性能,尤其是當(dāng)程序中需要大量生存期很短暫的線程時(shí),應(yīng)該考慮 ???? 使用線程池.所謂線程池是指在系統(tǒng)啟動(dòng)時(shí)即創(chuàng)建大量空閑的線程,程序?qū)⒁粋€(gè)Runnable對(duì)象或Callable對(duì)象傳給線程池,線程池就會(huì)啟動(dòng)一個(gè)線程來執(zhí)行他們的run或call方法,當(dāng)方法 ???? 結(jié)束時(shí),線程并不會(huì)死亡,而是返回線程池成為空閑狀態(tài),等待執(zhí)行下一個(gè)任務(wù) 定義: public interface Executor 方法:public void execute(Runnable command) Throws RejectedExecutionException | NullPointerException {}:Executes the given command at some time in the future 實(shí)現(xiàn)類:AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor 子接口:ExecutorService, ScheduledExecutorService