




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、會計學1第第 多線程多線程多個線程在多處理器上運行多個線程在單處理器上運行Thread 3 Thread 2 Thread 1 Thread 3 Thread 2 Thread 1 第1頁/共34頁 / Custom thread class public class CustomThread extends Thread . public CustomThread(.) . / Override the run method in Thread public void run() / Tell system how to run custom thread . . / Client clas
2、s public class Client . public void someMethod() . / Create a thread CustomThread thread1 = new CustomThread(.); / Start a thread thread1.start(); . / Create another thread CustomThread thread2 = new CustomThread(.); / Start a thread thread2.start(); . java.lang.Thread CustomThread 第2頁/共34頁TestThrea
3、dRun第3頁/共34頁 / Custom thread class public class CustomThread implements Runnable . public CustomThread(.) . / Implement the run method in Runnable public void run() / Tell system how to run custom thread . . / Client class public class Client . public void someMethod() . / Create an instance of Cust
4、omThread CustomThread customThread = new CustomThread(.); / Create a thread Thread thread = new Thread(customThread); / Start a thread thread.start(); . . java.lang.Runnable CustomThread 第4頁/共34頁TestRunnableRun第5頁/共34頁java.lang.Thread +Thread(tagert: Runnable) +run(): void +start(): void +interrupt(
5、): void +isAlive(): boolean +setPriority(p: int): void +join(): void +sleep(millis: long): void +yield(): void +isInterrupted(): Boolean +currentThread(): Thread java.lang.Runnable 第6頁/共34頁第7頁/共34頁第8頁/共34頁第9頁/共34頁 t2.join() -char token +getToken +setToken +paintComponet +mouseClicked Thread t1 -char
6、 token +getToken +setToken +paintComponet +mouseClicked Wait for t2 to finish +getToken +setToken +paintComponet +mouseClickeThread t2 -char token +getToken +setToken +paintComponet +mouseClicked t2 finished -char token +getToken (A) Using the join() method -char token anObj.wait() -char token +getT
7、oken +setToken +paintComponet +mouseClicked Thread t1 -char token +getToken +setToken +paintComponet +mouseClicked Wait for notification Thread t2 -char token +getToken +setToken +paintComponet +mouseClicked (B) Using the wait(), notify(), or notifyAll() -char token anObj.notify() -char token +getTo
8、ken +setToken +paintComponet +mouseClicked 第10頁/共34頁線程有五種狀態(tài):新建、就緒、運行、阻塞或結束。 新建 就緒 Thread created 結束 運行 start() run() 等待目標線程結束 join() run() returns yield(), or time out interrupt() 用完等待時間 等待通知 sleep() wait() 目標線 程結束 notify() or notifyAll() 時間到 阻塞 Interrupted() 第11頁/共34頁第12頁/共34頁第13頁/共34頁第14頁/共34頁Cloc
9、kClockControlClockAppRun ClockApp +AppletClock() +main(args: String): void JApplet -char token +getToken +setToken +paintComponet +mouseClicked ClockControl -clock: Clock jbtResume: JButton -jbtSuspend: JButton +ClockControl() +actionPerformed(e: ActionEvent): void JPanel -char token +getToken +setT
10、oken +paintComponet +mouseClicked Clock +Clock() +run(): void +suspend(): void +resume(): void StillClock -char token +getToken +setToken +paintComponet +mouseClicked Runnable 1 1 ActionListener 1 1 第15頁/共34頁ClockWithAudioRun第16頁/共34頁ClockWithAudioOnSeparateThreadRun第17頁/共34頁線程池是管理開發(fā)執(zhí)行很多任務的理想方法。JDK
11、1.5 使用 Executor 接口在線程池中執(zhí)行任務, ExecutorService 接口用來管理和控制任務。 ExecutorService 是 Executor的子接口. Shuts down the executor, but allows the tasks in the executor to complete. Once shutdown, it cannot accept new tasks. Shuts down the executor immediately even though there are unfinished threads in the pool. Re
12、turns a list of unfinished tasks. Returns true if the executor has been shutdown. Returns true if all tasks in the pool are terminated. interface java.util.concurrent.Executor +execute(Runnable object): void Executes the runnable task. interface java.util.concurrent.ExecutorService +shutdown(): void
13、 +shutdownNow(): List +isShutdown(): boolean +isTerminated(): boolean 第18頁/共34頁為了創(chuàng)建 Executor 對象, 使用 Executors 的靜態(tài)方法. Creates a thread pool with a fixed number of threads executing concurrently. A thread may be reused to execute another task after its current task is finished. Creates a thread pool t
14、hat creates new threads as needed, but will reuse previously constructed threads when they are available. java.util.concurrent.Executors +newFixedThreadPool(numberOfThreads: int): ExecutorService +newCachedThreadPool(): ExecutorService ExecutorDemoRun第19頁/共34頁 如果一個共享資源被多個線程同時訪問,可能會遭到破壞。Stepbalance t
15、hreadi threadj10 newBalance = bank.getBalance() + 1;20 newBalance = bank.getBalance() + 1;31 bank.setBalance(newBalance);41 bank.setBalance(newBalance);第20頁/共34頁AccountWithoutSyncRun Account -balance: int +getBalance(): int +deposit(amount: int): void 100 AccountWithoutSync -bank: Account -thread: T
16、hread +main(args: String): void java.lang.Object -char token +getToken +setToken +paintComponet +mouseClicked java.lang.Object -char token +getToken +setToken +paintComponet +mouseClicked AddAPennyThread +run(): void java.lang.Thread -char token +getToken +setToken +paintComponet +mouseClicked 1 1 1
17、 第21頁/共34頁 獲準對account對象加鎖 解除對account對象的鎖定 threadi 執(zhí)行deposit方法 獲準對account對象加鎖 執(zhí)行deposit方法 解除對account對象的鎖定 threadj - 等待對account對象加鎖 第22頁/共34頁第23頁/共34頁第24頁/共34頁 Same as ReentrantLock(false). Creates a lock with the given fairness policy. When the fairness is true, the longest-waiting thread will get th
18、e lock. Otherwise, there is no particular access order. interface java.util.concurrent.locks.Lock +lock(): void +unlock(): void +newCondition(): Condition Acquires the lock. Releases the lock. Returns a new Condition instance that is bound to this Lock instance. java.util.concurrent.locks.ReentrantL
19、ock +ReentrantLock() +ReentrantLock(fair: boolean) 第25頁/共34頁AccountWithSyncUsingLockRun第26頁/共34頁第27頁/共34頁 synchronized (anObject) try / Wait for the condition to become true while (!condition) anObject.wait(); / Do something when condition is true catch (InterruptedException ex) ex.printStackTrace()
20、; Thread 1 synchronized (anObject) / When condition becomes true anObject.notify(); or anObject.notifyAll(); . Thread 2 resume 第28頁/共34頁ThreadCooperationRun第29頁/共34頁 synchronized (object1) / do something here synchronized (object2) / do something here Thread 1 synchronized (object2) / do something here synchronized (object1) / do something here Thread 2 Step 1 2 3 4 5 6 Wait for Thread 2 to release
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 玻璃制造操作規(guī)程制度手冊
- 小班串串吧活動方案
- 巴黎活動策劃方案
- 居然之家周年活動方案
- 小數(shù)除法教案活動方案
- 帶領學生春游活動方案
- 幕墻公司開業(yè)活動方案
- 師生游戲大班活動方案
- 小班陶藝活動方案
- 干警猜燈謎活動方案
- 肢體離斷傷的護理
- 2025年中國高壓電源供應器行業(yè)市場調查、投資前景及策略咨詢報告
- 2024年中國黑龍江省農藥市場調查報告
- 注塑廠薪資管理制度
- 2025-2030年中國黑膠唱片行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- LINE6效果器HD300中文說明書
- 可編程控制技術課件
- 浙江省強基聯(lián)盟學考模擬2024-2025學年高二下學期6月學考模擬地理試題(含答案)
- 中國美術學院非教學崗位招聘筆試真題2024
- 外賣餐飲平臺管理制度
- 人形機器人深度研究系列八:諧波減速器:差齒傳動持續(xù)進化
評論
0/150
提交評論