1 / 17

第十章 多线程

第十章 多线程. 广东科学技术职业学院计算机工程技术学院 曾文权老师 Email : bless365@126.com. 本讲内容 【 知 识 点 】. 多线程的基本概念 线程类( Thread )及其方法 Runnable 接口 线程状态 —— 线程的生命周期. 本讲内容 【 案例 】. 并发显示字符串的简单例子 给程序添加一个时间显示控件 在程序窗口显示动画. 1. 多线程的基本概念. 并发现象在现代计算机中大量存在 操作系统提供并发机制 Java 提供语言级并发机制. 1. 多线程的基本概念. 程序:静态的计算机语言编写的代码。

drago
Download Presentation

第十章 多线程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十章 多线程 广东科学技术职业学院计算机工程技术学院曾文权老师Email:bless365@126.com

  2. 本讲内容【知 识 点】 • 多线程的基本概念 • 线程类(Thread)及其方法 • Runnable接口 • 线程状态——线程的生命周期

  3. 本讲内容【案例】 • 并发显示字符串的简单例子 • 给程序添加一个时间显示控件 • 在程序窗口显示动画

  4. 1. 多线程的基本概念 • 并发现象在现代计算机中大量存在 • 操作系统提供并发机制 • Java提供语言级并发机制

  5. 1. 多线程的基本概念 • 程序:静态的计算机语言编写的代码。 • 进程:程序的一次执行。 • 线程:程序中的部分代码的一次执行过程。 • 多进程:操作系统中多个程序同时执行。 • 多线程:程序中多个片断同时执行。

  6. 1.多线程的基本概念 进程 1 进程 2

  7. 多线程的优势 • 多线程编程简单,效率高 • 能直接共享数据和资源(多进程不能) • 适合于开发服务程序 • 如Web服务,聊天服务等 • 适合于开发有多种交互接口的程序 • 如聊天程序的客户端,网络下载工具 • 适合于有人机交互又有计算量的程序 • 如字处理程序(Word,Excel)

  8. 2. 线程体的创建方法 • 声明一个 Thread 类的子类,并覆盖run()方法 • 声明一个实现 Runnable 接口的类,并实现run()方法

  9. 创建方法 1 声明一个 Thread 类的子类,并覆盖 run() 方法。 class mythread extends Thread { // 覆盖run方法 public void run( ) {…… } • }

  10. 创建方法 2 声明一个实现 Runnable 接口的类,并实现 run() 方法。 class mythread implements Runnable { // 实现run方法 public void run( ) {…… } • }

  11. 线程类(Thread)的主要方法 • Thread(String threadName) • 构造方法,并给线程指定一个名字 • Thread() • 构造方法, 让系统缺省设置线程名(Thread-#) • void run() • 线程所执行的代码 • void start() throws IllegalThreadStateException • 使程序开始执行,多次调用会产生异常 • void sleep(long milis) • 让线程睡眠一段时间,此期间线程不消耗CPU资源

  12. 线程类(Thread)的其它方法 • void interrupt() • 中断线程, • static boolean interrupted() • 判断当前线程是否被中断(会清除中断状态标记) • boolean isInterrupted() • 判断指定线程是否被中断 • boolean isAlive() • 判断线程是否处于活动状态(即已调用start,但run还未返回) • Thread currentThread() • 返回当前线程对象的引用

  13. 线程类(Thread)的其它方法 • void setName(String threadName) • 改变线程的名字 • String getName() • 获得线程的名字 • String toString() • 例子:Thread[Thread-0,1,test] • void join([longmillis[, intnanos]]) • 等待线程结束

  14. 线程状态—线程的生命周期 born start ready wait interval expires notify notifyAll interrupt quantum expiration yield dispatch I/O complete running issue I/O request wait run complete sleep waiting blocked sleeping dead sleep interval expires interrupt

  15. 线程状态—线程的生命周期 • 新建: 新建的线程处于新建状态 • 就绪: 在创建线程后,它将处于就绪状态,等待 start() 方法被调用 • 执行: 线程在开始执行时进入运行状态 • 阻塞: 在线程等待一个事件时(例如输入/输出操作),就称其处于阻塞状态。 • 死亡: 在 run() 方法已完成执行或其 stop() 方法被调用之后,线程就处于死亡状态。

  16. 关于Runnable接口 • 使得其它非Thread派生类也可以声明线程所应完成的任务(实现run方法) • Thread(Runable ro) • Thread(Runable ro, String threadName)

  17. 下课!

More Related