Java多线程管理课件

上传人:我*** 文档编号:144988138 上传时间:2020-09-15 格式:PPT 页数:23 大小:483KB
返回 下载 相关 举报
Java多线程管理课件_第1页
第1页 / 共23页
Java多线程管理课件_第2页
第2页 / 共23页
Java多线程管理课件_第3页
第3页 / 共23页
Java多线程管理课件_第4页
第4页 / 共23页
Java多线程管理课件_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《Java多线程管理课件》由会员分享,可在线阅读,更多相关《Java多线程管理课件(23页珍藏版)》请在金锄头文库上搜索。

1、第三讲,线程管理,本章目标,线程的优先级: 优先级概述 线程优先级的具体应用 线程同步: 线程同步的目的 线程同步的具体应用 线程死锁: 产生死锁的必要条件与解决方法 wait和notify机制,线程优先级,多线程运行时需要定义线程运行的先后顺序 线程优先级是用数字表示,数字越大线程优先级越高,取值在(1到10)。 默认优先级(为5)。,优先级应用一,public class PriThread public static void main(String args ) ThreadA a=new ThreadA(); ThreadB b=new ThreadB(); a.setPriorit

2、y(2);/设置优先级别,数值越大优先级越高 b.setPriority(3); a.start(); b.start(); ,优先级应用二,class ThreadA extends Thread public void run() System.out.println(我是线程A); class ThreadB extends Thread public void run() System.out.println(我是线程B); 因为在代码段当中我们把线程B的优先级设置高于线程A,所以运行结果先执行线程B的run()方法后再执行线程A的run()方法。,线程优先级的获得,JAVA中获得线程

3、优先级的方法,是通过getPriority()方法来实现的。 public class PriThread public static void main(String args ) Thread a=new Thread(); Thread b=new Thread(); int priA=a.getPriority();/获得优先级的方法 int priB=b.getPriority(); System.out.println(priA); System.out.println(priB); ,线程常量设置优先级,设置优先级也可以用线程常量。 MAX_PRIORITY为最高优先级10; M

4、IN_PRIORITY为最低优先级1; NORM_PRIORITY是默认优先级5。,线程常量设置优先级示例,public class PriConstant public static void main(String args ) Thread a=new Thread(); int temp=Thread.MAX_PRIORITY; a.setPriority(temp);/设置此线程优先级最高 System.out.println(a.getPriority(); temp=Thread.MIN_PRIORITY; a.setPriority(temp);/设置此线程优先级最低 Syst

5、em.out.println(a.getPriority(); temp=Thread.NORM_PRIORITY; a.setPriority(temp);/将线程优先级设置为默认 System.out.println(a.getPriority(); ,线程安全问题,public class Piao public int num; public Piao(int num) this.num = num; public void sell(String name) if(num=0) return; System.out.println(name+卖+num); try Thread.sl

6、eep(10); catch (InterruptedException e) e.printStackTrace(); num=num-1; ,安全问题的解决,Java中嵌套同步是安全的 同步化方法 同步块的方式: void method() synchronized(this) / 同步方法: synchronized void method() / ,同步原理,synchronized (object) / 钥匙在对象中,而不在代码中。 每个对象有一个钥匙 为了执行synchronized()块,线程需要得到对象中的钥匙。一旦获得了钥匙,对象就不再拥有钥匙。 如果当线程要执行synchr

7、onized()时,钥匙不在对象中,线程就wait。一直到钥匙还到了对象中,才被这个线程拿到。 当线程离开synchorized()块,钥匙就还给了对象。,阶段回顾,线程优先级的概念? 什么是线程同步?如何实现线程同步? 实现同步的要点: 两个线程对相同的公共资源进行访问。 通过同步公共资源的访问方法实现线程安全。,死锁的必要条件与解决方法,死锁图,P1、P2表示两个线程,R1、R2表示资源,P1已经占用资源R1而且在等待R2,P2已经占用资源R2而且在等待R1,这时就会产生两个线程互相等待的状态。,wait和notify机制,实际应用中,多线程之间常常需要互相协调工作。例如生产者和消费者的问

8、题。 在JAVA中,这个机制的实现依赖于wait()/notify()方法。 synchronized(obj) while(!condition) obj.wait(); obj.doSomething(); ,Wait与notify示例,以生产产品面包为例。 class Bread private int id; public Bread(int id) this.id = id; public String toString() return 面包+id; ,Wait与notify示例,面包是放在一个篮子中,篮子满了生产者必须等待,篮子空了,消费者必须等待。两者之间互相要有通讯。 cla

9、ss Basket private Bread breads = new Bread6; private int index = 0; ,Wait与notify示例,生产方法push public synchronized void push(Bread bread) if(index = breads.length) try this.wait(); catch (InterruptedException e) e.printStackTrace(); breadsindex = bread; index+; System.out.println(生产了+bread); this.notif

10、y(); ,Wait与notify示例,消费方法pop() public synchronized void pop() try Thread.sleep(1); catch (InterruptedException e1) e1.printStackTrace(); if(index = 0) try this.wait(); catch (InterruptedException e) e.printStackTrace(); index-; Bread bread = breadsindex; System.out.println(消费了+bread); this.notify();

11、,Wait与notify示例,生产者 class Producer implements Runnable private Basket basket; public Producer(Basket basket) this.basket = basket; public void run() for(int i=0;i20;i+) Bread bread = new Bread(i); basket.push(bread); ,Wait与notify示例,消费者 class Consumer implements Runnable private Basket basket; public

12、Consumer(Basket basket) this.basket = basket; public void run() for(int i=0;i20;i+) basket.pop(); ,Wait与notify示例,测试类: public class TestProducerConsumer public static void main(String args) Basket basket = new Basket(); Producer producer = new Producer(basket); Consumer consumer = new Consumer(basket); Thread thread = new Thread(producer); Thread thread2 = new Thread(consumer); thread.start(); thread2.start(); ,本章总结,1线程同步有那些方法来实现? 2什么情况下会出现死锁?,作业,上机作业 通过wait和notify完成生产者和消费者问题 理论作业 为什么需要线程同步,通过什么方式来实现线程同步 线程在什么时候会发生死锁,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

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