java课程总结第14章

上传人:飞****9 文档编号:130104537 上传时间:2020-04-25 格式:PPT 页数:32 大小:137.50KB
返回 下载 相关 举报
java课程总结第14章_第1页
第1页 / 共32页
java课程总结第14章_第2页
第2页 / 共32页
java课程总结第14章_第3页
第3页 / 共32页
java课程总结第14章_第4页
第4页 / 共32页
java课程总结第14章_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《java课程总结第14章》由会员分享,可在线阅读,更多相关《java课程总结第14章(32页珍藏版)》请在金锄头文库上搜索。

1、第十四章 多线程 14 1Java的多线程机制 多线程机制是Java语言的又一重要特征 使用多线程技术可以使系统同时运行多个执行体 这样可以加快程序的响应时间 提高计算机资源的利用率 使用多线程技术可以提高整个应用系统的性能 14 1 1线程的生命周期 每个Java程序都有一个主线程 即main 方法对应的线程 要实现多线程 必须在主线程中创建新的线程 在Java语言中 线程用Thread类及其子类的对象来表示 每个线程要经历由 新生 就绪 运行 阻塞 死亡 5种状态 线程从新生到死亡的状态变化过程称为生命周期 线程的生命周期 新生 就绪 阻塞 运行 死亡 1 新生状态 用new关键字和Thr

2、ead类或其子类建立一个线程对象后 该线程对象就处于新生状态 处于新生状态的线程有自己的内存空间 通过调用start方法进入就绪状态 2 就绪状态 处于就绪状态的线程已经具备了运行条件 但还没有分配到CPU 因而将进入线程队列 等待系统为其分配CPU 一旦获得CPU 线程就进入运行状态并自动调用自己的run方法 3 运行状态 在运行状态的线程执行自己的run方法中代码 直到调用其他方法而终止 或等待某资源而阻塞或完成任务而死亡 4 阻塞状态 处于运行状态的线程在某些情况下 如执行了sleep 睡眠 方法 或等待I O设备等资源 将让出CPU并暂时终止自己的运行 进入阻塞状态 在阻塞状态的线程不

3、能进入就绪队列 只有当引起阻塞的原因消除时 如睡眠时间已到 或等待的I O设备空闲下来 线程便转入就绪状态 重新到就绪队列中排队等待CPU资源 当再次获得CPU时 便从原来终止位置开始继续运行 5 死亡状态 死亡状态是线程生命周期中的最后一个阶段 线程死亡的原因有两个 一个是正常运行的线程完成了它的全部工作 另一个是线程被强制性地终止 如通过执行stop或destroy方法来终止一个线程 14 1 2多线程的实现方法 在Java中 创建线程的方法有两种 通过创建Thread类的子类来实现 通过实现Runnable接口的类来实现 14 2通过Thread类实现多线程 通过继承Thread类实现多

4、线程的方法是首先设计Thread的子类 然后根据工作需要重新设计线程的run方法 再使用start方法启动线程 将执行权转交到run Java实例 通过Thread类 通过继承Thread类实现多线程classThread1extendsThread Strings intm count 0 Thread1 Stringss intmm s ss m mm publicvoidrun try while true System out print s sleep m count Java实例 续 if count 20 break System out println s finished c

5、atch InterruptedExceptione return publicstaticvoidmain Stringargs Thread1threadA newThread1 A 50 Thread1threadB newThread1 B 100 threadA start threadB start 运行该程序的输出结果如下所示 ABABAABAABAABAABAABAABAABAABAABAfinished BBBBBBBBBBfinished 14 3通过Runnable接口实现多线程 通过Runnable接口实现多线程的方法 设计一个实现Runnable接口的类 然后根据工作

6、需要重新设计线程的run方法 建立该类的对象 以此对象为参数建立Thread类的对象 调用Thread类对象的start方法启动线程 将执行权转交到run方法 Java实例 通过Runnable接口 通过Runnable接口实现多线程classThread2implementsRunnable Strings intm count 0 Thread2 Stringss intmm s ss m mm publicvoidrun try while true System out print s Thread sleep m if count 20 break Java实例 续 System o

7、ut println s hasfinished catch InterruptedExceptione return publicstaticvoidmain Stringargs Thread2threadA newThread2 A 50 Thread2threadB newThread2 B 100 ThreadthreadC newThread threadA ThreadthreadD newThread threadB threadC start threadD start 运行该程序的输出结果如下所示 ABABAABAABAABAABAABAABAABAABAABAhasfin

8、ished BBBBBBBBBBhasfinished 14 4线程等待 Java程序中的线程并发运行 共同争抢CPU资源 哪个线程抢夺到CPU资源后 就开始运行 线程的调度执行是按照其优先级高低的顺序进行的 为了防止高级线程未完成 低级线程没有机会获得CPU 使低级线程有机会执行 那么让高级线程暂时休眠一段时间 使用sleep 方法 休眠时间的长短由sleep 方法中的参数决定 Java实例 线程等待 例 各线程的并发执行classThread3extendsThread Strings intm i 0 Thread3 Stringss s ss publicvoidrun try for

9、 i 0 i 6 i sleep int 500 Math random System out println s Java实例 续 System out println s finished catch InterruptedExceptione return publicstaticvoidmain Stringargs Thread3threadA newThread3 A Thread3threadB newThread3 B threadA start threadB start System out println mainisfinished mainisfinishedBAAB

10、ABABBABBfinished AAfinished 例 join 方法的功能classThread4extendsThread Strings intm i 0 Thread4 Stringss s ss publicvoidrun try for i 0 i 6 i sleep int 500 Math random System out println s System out println s finished catch InterruptedExceptione return publicstaticvoidmain Stringargs Thread4threadA newT

11、hread4 A Thread4threadB newThread4 B threadA start try threadA join catch InterruptedExceptione threadB start System out println mainisfinished 运行该程序的输出结果如下所示 AAAAAAAfinished mainisfinishedBBBBBBBfinished 14 5线程同步 Java提供了多线程机制 通过多线程的并发运行可以提高系统资源利用率 改善系统性能 但在有些情况下 一个线程必须和其他线程合作才能共同完成任务 线程可以共享内存 利用这个特

12、点可以在线程之间传递信息 在Java中 实现同步操作的方法是在共享内存变量的方法前加synchronized修饰符 在程序运行过程中 如果某一线程调用经synchronized修饰的方法 在该线程结束此方法的运行之前 其他所有线程都不能运行该方法 只有等该线程完成此方法的运行后 其他线程才能y运行该方法 例 线程并发引起的不确定性classCbank privatestaticints 2000 publicstaticvoidsub intm inttemp s temp temp m try Thread sleep int 1000 Math random catch Interrupt

13、edExceptione s temp System out println s s classCustomerextendsThread publicvoidrun for inti 1 i 4 i Cbank sub 100 publicclassThread5 publicstaticvoidmain Stringargs Customercustomer1 newCustomer Customercustomer2 newCustomer customer1 start customer2 start 其中一次运行的显示结果 s 1900s 1800s 1900s 1700s 1800

14、s 1700s 1600s 1600 Java实例 线程同步 例 线程的同步classCbank privatestaticints 2000 publicsynchronizedstaticvoidsub intm inttemp s temp temp m try Thread sleep int 1000 Math random catch InterruptedExceptione s temp System out println s s Java实例 续 classCustomerextendsThread publicvoidrun for inti 1 i 4 i Cbank sub 100 publicclassThread5 publicstaticvoidmain Stringargs Customercustomer1 newCustomer Customercustomer2 newCustomer customer1 start customer2 start 该程序的运行结果如下 s 1900s 1800s 1700s 1600s 1500s 1400s 1300s 1200

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 总结/报告

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号