收藏 分享(赏)

java多线程ppt课件.pptx

上传人:微传9988 文档编号:2507735 上传时间:2018-09-19 格式:PPTX 页数:18 大小:441.57KB
下载 相关 举报
java多线程ppt课件.pptx_第1页
第1页 / 共18页
java多线程ppt课件.pptx_第2页
第2页 / 共18页
java多线程ppt课件.pptx_第3页
第3页 / 共18页
java多线程ppt课件.pptx_第4页
第4页 / 共18页
java多线程ppt课件.pptx_第5页
第5页 / 共18页
点击查看更多>>
资源描述

1、This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller

2、and DS18B20 temperature sensor.JAVA多线程This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of t

3、he system are AT89C51 microcontroller and DS18B20 temperature sensor.线程的基本概念线程的创建和启动线程的调度和优先级线程的状态控制线程同步JAVA SE基础This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital tempera

4、ture sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.线程的基本概念 线程是一个程序内部的顺序控制流。 线程和进程的区别 每个进程都有独立的代码和数据空间 (进程上下文 ),进程间的切换会有较大的开销。 线程可以看成时轻量级的进程,同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器 (PC),线程切换的开销小。 多进程 : 在操作系统中能同时运行多个

5、任务 (程序 ) 多线程 : 在同一应用程序中有多个顺序流同时执行n Java的线程是通过 java.lang.Thread类来实现的。n VM 启动时会有一个由主方法( public static void main() )所定义的线程。n 可以通过创建 Thread 的实例来创建新的线程。n 每个线程都是通过某个特定 Thread对象所对应的方法 run( )来完成其操作的,方法 run( )称为线程体。n 通过调用 Thead类的 start()方法来启动一个线程。This paper mainly introduces the design of an intelligent temp

6、erature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.线程的创建和启动 可以有两种方式创建新的线程。 第一种 定义线

7、程类实现 Runnable接口 Thread myThread new Thead( target) /target为Runnable接口类型。 Runnable中只有一个方法: public void run() ; 用以定义线程运行体。 使用 Runnable接口可以为多个线程提供共享的数据。 在实现 Runnable接口的类的 run方法定义中可以使用 Thread的静态方法: public static Thread currentThread() 获取当前线程的引用。 第二种 可以定义一个 Thread的子类并重写其 run方法如:class MyThread extends The

8、ad public void run() 然后生成该类的对象:MyThread myThread new MyThead() 使用那种好呢?This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microco

9、mputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.线程状态转换This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature s

10、ensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.方 法 功 能isAlive() 判断线程是否还 “ 活 ” 着,即线程是否还未终止。getPriority() 获得线程的优先级数值setPriority() 设置线程的优先级数值Thread.sleep() 将当前线程睡眠指定毫秒数join() 调用某线程的该方法,将当前线程与该线程 “ 合并 ” ,即等待该线程结束,

11、再恢复当前线程的运行。yield() 让出 CPU, 当前线程进入就绪队列等待调度。wait() 当前线程进入对象的 wait pool。notify()/notifyAll() 唤醒对象的 wait pool中的一个 /所有等待线程。线程状态转换This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digi

12、tal temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.sleep / join / yield 方法 sleep方法 可以调用 Thread的静态方法:public static void sleep(long millis) throws InterruptedException使得当前线程休眠(暂时停止执行 millis毫秒)。 由于是静

13、态方法, sleep可以由类名直接调用:Thread.sleep() join方法 合并某个线程 yield方法 让出 CPU,给其他线程执行的机会This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip mic

14、rocomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.线程模式 两种线程模式: 协作式 :一个线程保留对处理器的控制直到它自己决定放弃 速度快、代价低 用户编程非常麻烦 抢先式 。系统可以任意的从线程中夺回对 CPU的控制权,再把控制权分给其它的线程 。 两次切换之间的时间间隔就叫做 时间片 效率不如协作式高 , OS核心必须负责管理线程 简化编程,而且使程序更加可靠 多数线程的调度是抢先式的。This paper mainly intro

15、duces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature

16、 sensor.线程的优先级别 Java提供一个 线程调度器 来监控程序中启动后进入就绪状态的所有线程。线程调度器按照线程的优先级决定应调度哪个线程来执行。 线程的优先级用数字表示,范围从 1到 10,一个线程的缺省优先级是 5。 Thread.MIN_PRIORITY = 1 Thread.MAX_PRIORITY = 10 Thread.NORM_PRIORITY = 5 使用下述线方法获得或设置线程对象的优先级。 int getPriority(); void setPriority(int newPriority); 不同平台上的优先级 Solaris:相同优先级的线程不能相互抢占对方

17、的 cpu时间。 windows:可以抢占相同甚至更高优先级的线程的 cpu时间This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of

18、 the system are AT89C51 microcontroller and DS18B20 temperature sensor.临界资源问题 (1) 两个线程 A和 B在同时操纵 Stack类的同一个实例 (堆栈 ), A正在往堆栈里 push一个数据, B则要从堆栈中 pop一个数据。class Stackint idx=0;char data = new char6;public void push(char c)dataidx = c;idx+;public char pop()idx-;return dataidx;This paper mainly introduces

19、 the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sens

20、or.临界资源问题 (2)1. 操作之前 data = | a | b | | | | | idx=22. A执行 push中的第一个语句,将 c推入堆栈;data = | a | b | c | | | | idx=23. A还未执行 idx+语句, A的执行被 B中断, B执行 pop方法,返回 c:data = | a | b | c | | | | idx=14. A继续执行 push的第二个语句:data = | a | b | c | | | | idx=2最后的结果相当于 c没有入栈 , 产生这种问题的原因在于对共享数据访问的操作的不完整性。This paper mainly i

21、ntroduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 tempera

22、ture sensor.线程同步public class Test implements Runnable Timer timer = new Timer();public static void main(String args) Test test = new Test();Thread t1 = new Thread(test);Thread t2 = new Thread(test);t1.setName(“t1“); t2.setName(“t2“);t1.start(); t2.start();public void run()timer.add(Thread.currentThr

23、ead().getName();class Timerprivate static int num = 0;public void add(String name)num +;try Thread.sleep(1); catch (InterruptedException e) System.out.println(name+“, 你是第 “+num+“个使用 timer的线程 “);This paper mainly introduces the design of an intelligent temperature control system which realizes the fu

24、nction of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.Synchronized总结 无论 synchronized关键字加在方法上还是对象上,它取得的锁都是锁在了对象上,而不是把一段代码或函数当作锁 而且

25、同步方法很可能还会被其他线程的对象访问。 每个对象只有一个锁( lock)与之相关联。 实现同步是要很大的系统开销作为代价的,甚至可能造成死锁,所以尽量避免无谓的同步控制。 搞清楚 synchronized锁定的是哪个对象,就能帮助我们设计更安全的多线程程序。 还有一些技巧可以让我们对共享资源的同步访问更加安全: 定义 private 的 instance变量 +它的 get方法,而不要定义public/protected的 instance变量。如果将变量定义为 public,对象在外界可以绕过同步方法的控制而直接取得它,并改动它。 如果 instance变量是一个对象,如数组或 Array

26、List什么的,那上述方法仍然不安全,因为当外界对象通过 get方法拿到这个 instance对象的引用后,又将其指向另一个对象,那么这个 private变量也就变了,岂不是很危险。 这个时候就需要将 get方法也加上 synchronized同步,并且,只返回这个 private对象的 clone()这样,调用端得到的就是对象副本的引用了。This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature

27、measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.线程同步 在 Java语言中,引入了对象互斥锁的概念,保证共享数据操作的完整性。每个对象都对应于一个可称为 “ 互斥锁 ” 的标记,这个标记保证在任一时刻,只能有一个线程访问该对象。 关键字 s

28、ynchronized 来与对象的互斥锁联系。当某个对象 synchronized修饰时,表明该对象在任一时刻只能由一个线程访问。 synchronized(this)num +;try Thread.sleep(1); catch (InterruptedException e) System.out.println(name+“, 你是第 “+num+“个使用 timer的线程 “); synchronized 的使用方法: synchronized 还可以放在方法声明中,表示整个方法为同步方法,例如:synchronized public void add(String name)Thi

29、s paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and

30、 DS18B20 temperature sensor.面试: Wait sleep区别 来源不同 Sleep是 Thread提供的方法 Wait继承自 Object 代码位置不同 Wait需要写在 Synchronize语句块里面 是否释放锁定对象 调用 wait方法,释放锁定该对象 Sleep时别的线程也不可以访问锁定对象This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurem

31、ent and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.总结 线程、进程的概念 线程的创建和启动方式 线程的调度和优先级 Sleep Join Yield synchronized Wait Notify、 notifyAllThis paper mainly in

32、troduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperat

33、ure sensor.Q&AAny Question?This paper mainly introduces the design of an intelligent temperature control system which realizes the function of temperature measurement and control by using single bus digital temperature sensor DS18B20 and single chip microcomputer. The core components of the system are AT89C51 microcontroller and DS18B20 temperature sensor.www.C谢谢大家

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 中等教育 > 小学课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报