




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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页线程有五种状态:新建、就绪、运行、阻塞或结束。 新建 就绪 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页线程池是管理开发执行很多任务的理想方法。JDK
11、1.5 使用 Executor 接口在线程池中执行任务, 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页为了创建 Executor 对象, 使用 Executors 的静态方法. 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 执行deposit方法 获准对account对象加锁 执行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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中级经济学考试试题及答案
- 2025年基础电路分析考试试题及答案对比
- 2025年职业规划与生涯发展考试试卷及答案
- 2025年电子电气工程师考试试卷及答案研究
- 2025年创业管理与创新课程考试试题及答案
- 2025年临床心理师执业考试试题及答案
- 全球货运保险理赔争议调解及赔偿协议
- 房产使用权限变更及物业管理责任转移合同
- 教育培训招生代理服务及课程研发合同
- 智能机器人制造工厂普工劳务服务协议
- 《集成电路基础及其应用》课件
- 2025年保密观知识竞赛题库完整答案带答案详解
- 大部分分校:地域文化形考任务三-国开(CQ)-国开期末复习资料
- 超星尔雅学习通《当代大学生国家安全教育》章节测试答案
- 《人工智能基础》课件-AI的前世今生:她从哪里来
- ISO28000:2022供应链安全管理体系
- 教练技术三阶段讲义
- 深圳市失业人员停止领取失业保险待遇申请表样表
- JIS G4305-2021 冷轧不锈钢板材、薄板材和带材
- 小型玉米脱粒机的设计毕业设计
- 铝母线设计装配技术要求
评论
0/150
提交评论